Program Graph
Program Graph
Partition ADT and implementation (4.2.2) The decorator pattern (6.5.1) The traveling salesperson problem
6/8/2002 2:07 PM
Campus Tour
6/8/2002 2:07 PM
Campus Tour
Graph Assignment
Goals
a u
b w
Learn and implement the adjacency matrix structure an Kruskals minimum spanning tree algorithm Understand and use the decorator pattern and various JDSL classes and interfaces Implement the adjacency matrix structure for representing a graph Implement Kruskals MST algorithm Computation and visualization of an approximate traveling salesperson tour
Campus Tour 3
Your task
Frontend
Reference to edge object for adjacent vertices Null for non nonadjacent vertices
6/8/2002 2:07 PM
6/8/2002 2:07 PM
Campus Tour
Kruskals Algorithm
The vertices are partitioned into clouds
Example
B 1 A 5 7 10 8 9 C 11 E 3 D H G 4 6 2 A F 1 B 5 7 10 8 9 C 11 E 3 D H G 4 6 2 F
We start with one cloud per vertex Clouds are merged during the execution of the algorithm makeSet(o): create set {o} and return a locator for object o find(l): return the set of the object with locator l union(A,B): merge sets A and B
Algorithm KruskalMSF(G) Input weighted graph G Output labeling of the edges of a minimum spanning forest of G Q new heap-based priority queue for all v G.vertices() do l makeSet(v) { elementary cloud } setLocator(v,l) for all e G.edges() do Q.insert(weight(e), e) while Q.isEmpty() e Q.removeMin() [u,v] G.endVertices(e) A find(getLocator(u)) B find(getLocator(v)) if A B setMSFedge(e) { merge clouds } union(A, B)
5
Partition ADT:
B 1 A 5 7
8 9 C 11 10 E 3 D
G 4 6 H 2 A
Campus Tour
B F 1 5 7
8 9 C 11 10 E 3 D
G 4 6 H 2 F
6/8/2002 2:07 PM
Campus Tour
6/8/2002 2:07 PM
Example (contd.)
B 1 A 5 7 10 8 9 C 11 E 3 D H G 4 6 2 A F 1 B 5 7 10 8 9 C 11 E 3 D H G 4 6 2 F
Partition Implementation
Partition implementation
st e
ps
four steps
B 1 A 5 7 10 8 9 C 11 E 3 D H G 4 6 2 F
B 1 A 5 7
8 9 C 11 10 E 3 D
4 6 H 2 F
tw o
A set is represented the sequence of its elements A position stores a reference back to the sequence itself (for operation find) The position of an element in the sequence serves as locator for the element in the set In operation union, we move the elements of the smaller sequence into to the larger sequence makeSet, find: O(1) union: O(min(nA, nB))
Campus Tour
Amortized analysis
Consider a series of k Partiton ADT operations that includes n makeSet operations Each time we move an element into a new sequence, the size of its set at least doubles An element is moved at most log2 n times Moving an element takes O(1) time The total time for the series of operations is O(k + n log n)
8
6/8/2002 2:07 PM
Campus Tour
6/8/2002 2:07 PM
Decorator Pattern
Labels are commonly used in graph algorithms
Methods vertices and edges are called once Method endVertices is called m times We perform m insert operations and m removeMin operations We perform n makeSet operations, 2m find operations and no more than n 1 union operations
Auxiliary data Output DFS: unexplored/visited label for vertices and unexplored/ forward/back labels for edges Dijkstra and Prim-Jarnik: distance, locator, and parent labels for vertices Kruskal: locator label for vertices and MSF label for edges
Campus Tour
The decorator pattern extends the methods of the Position ADT to support the handling of attributes (labels)
Examples
Partition operations
Label operations
Kruskals algorithm runs in time O((n + m) log n) time provided the graph has no parallel edges and is represented by the adjacency list structure
6/8/2002 2:07 PM Campus Tour 9
has(a): tests whether the position has attribute a get(a): returns the value of attribute a set(a, x): sets to x the value of attribute a destroy(a): removes attribute a and its associated value (for cleanup purposes)
The decorator pattern can be implemented by storing a dictionary of (attribute, value) items at each position
10
6/8/2002 2:07 PM
TSP Approximation
4
B 2 A 5 6 C
7 2 8 1
D F E 3
We can approximate a TSP tour with a tour of at most twice the weight for the case of Euclidean graphs
Find a polynomial-time algorithm computing a traveling salesperson tour or prove that none exists
Campus Tour
Vertices are points in the plane Every pair of vertices is connected by an edge The weight of an edge is the length of the segment joining the points Compute a minimum spanning tree Form an Eulerian circuit around the MST Transform the circuit into a tour
Campus Tour 12
Approximation algorithm
6/8/2002 2:07 PM
11
6/8/2002 2:07 PM