Assignment 5: Prolog

Background

This assignment is for extra credit only.

Start with a set of numbers 1 .. n. Initially, each number is distinct from all the others. There are two operations: merge and query. Merging two numbers means making them indistinct from each other, that is, placing them in the same "group". Querying two numbers means answering the yes/no question: Are these two numbers in the same group? For instance, consider this sequence of operations:

 	merge(1,2).
 	merge(4,5).
 	query(1,2).  % returns yes
 	query(1,4).  % returns no
 	merge(2,5).
 	query(1,5).  % returns yes
 	query(1,3).  % returns no
 	query(1,4).  % returns yes
There is a standard algorithm and data structure for this problem, known as the Union-Find algorithm. However, you may use brute-force search.

The assignment

Write a Prolog program that implements merge and query. The merge operations can be represented by a file of facts in the form shown above. You can cause this file to be read in by giving the input

 	['filename'] .
You must implement query as a Prolog predicate.

How to submit

Turn in your program and your own test data in the usual way. Also run your program the class test data in
http://www.cs.uky.edu/~raphael/courses/CS450/asg.prolog.data.

Extra credit

  1. Any working algorithm gets at least 0.5 extra credit points.
  2. If your algorithm implements Union-Find without path compression or good choice of direction for inserting edges, you can get 1.0 extra credit points. You will need to use assert.
  3. If your algorithm implements Union-Find with a good choice of direction, you can get 1.5 extra credit points.
  4. If your algorithm implements Union-Find with a good choice of direction and partial path compression, you can get 2.0 extra credit points.