Programming Assignment 3: Graph Matching

Due: Friday March 12 at midnight

ReadGraph.java
some Java code to help you read a graph from a file, also check out Sample.java
ReadGraph.c
some C++ code to help you read a graph from a file, also check out Sample.cpp
graphs.zip
several graph data files for testing your program
pictures.zip
pictures of the above graphs, useful for debugging
c4.gif
replacement picture of graph c4

Overview

This project is related to the problem of recognizing objects in images. Given a known object and a new image, we want to determine whether or not that object is present in the image. Both the image and the object are represented as graphs, where a node corresponds to a region of pixels and the edges correspond to adjacencies between regions.

Suppose we have a model graph GM (the object) and an image graph GD (the image). With this representation, the problem reduces to: determining if there is a subgraph of GD isomorphic to GM (for perfect matches).

Suppose GM has vertices {1,2,3,...,n1} and GD has vertices {1,2,3,...,n2}, where n1 <= n2. Then, a mapping f(v) can be expressed as a set of pairs {(1,i1),(2,i2),(3,i3),(n1,in1)} such that every vertex in GM is mapped to exactly one vertex in GD. For a subgraph isomorphism, the number of edges (u,v) in GM for which (f(u),f(v)) is not an edge in GD MUST BE ZERO. Your program will find all subgraph isomorphisms from the model graph GM to the image graph GD.

What You Need To Do

Implement a backtracking tree search to find all copies of GM in GD. In other words, find all mappings with zero error. If there are none, output the message "NO ISOMORPHISMS".

Write a program that can be executed from the command line with

	Match gmfile gdfile 

for C++, or

	java Match gmfile gdfile 

for Java. The gmfile and gdfile arguments should be the names of two files that contain graphs (see the format specified below).

Details

Reading the Graphs

You will need to read the graphs from two files, one for each graph. The graphs are undirected, and the nodes are labeled between 1 and the number of nodes, which is specified in the file. The file format is as follows:

	GRAPH NAME (a string)
	N (the number of nodes, an integer)
	M (the number of edges, an integer)
	U1 V1 (two node labels specifying an edge, both integers)
	U2 V2
	U3 V3
	  .
	  .
	  .
	UM VM

For example, a graph file might look like:

triangle
3
3
1 2
1 3
2 3

or

kite
4
4
1 2
2 3
2 4
3 4

Output

Suppose the above two graphs are stored in files triangle.graph and kite.graph. Then, the command

	Match triangle.graph kite.graph 

should output

	ISOMORPHISM: (1,2),(2,3),(3,4)
	ISOMORPHISM: (1,2),(2,4),(3,3)
	ISOMORPHISM: (1,3),(2,2),(3,4)
	ISOMORPHISM: (1,3),(2,4),(3,2)
	ISOMORPHISM: (1,4),(2,2),(3,3)
	ISOMORPHISM: (1,4),(2,3),(3,2)

If there are no isomorphisms, output NO ISOMORPHISMS on a separate line with no punctuation.

What To Turn In

You should turn in the following:

For your own amusement, you can try

All of the above files are included in the graphs.zip archive. You can use the triangle and kite graphs to help debug your program.

And Remember.....

Some sample outputs for your comparison

Comparing k5.graph to usa.graph gives this
Comparing triangle.graph to k4.graph gives this
Comparing small_house.graph to usa.graph gives this