Outline and Reading Campus Tour
Overview of the assignment Review
Adjacency matrix structure (6.2.3) Kruskals MST algorithm (7.3.1)
Partition ADT and implementation (4.2.2) The decorator pattern (6.5.1) The traveling salesperson problem
Definition Approximation algorithm (13.4.3)
6/8/2002 2:07 PM
Campus Tour
6/8/2002 2:07 PM
Campus Tour
Graph Assignment
Goals
Adjacency Matrix Structure
Edge list structure Augmented vertex objects
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
Integer key (index) associated with vertex 0 u 1 0 0 1 a 2 b v 1 2 2 w
Your task
2D-array adjacency array
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 [Link]() do l makeSet(v) { elementary cloud } setLocator(v,l) for all e [Link]() do [Link](weight(e), e) while [Link]() e [Link]() [u,v] [Link](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
Worst-case running times
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
Analysis of Kruskals Algorithm
Graph operations
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
Priority queue 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
We set vertex labels n times and get them 2m times
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
Traveling Salesperson Problem
A tour of a graph is a spanning cycle (e.g., a cycle that goes through all the vertices) A traveling salesperson tour of a weighted graph is a tour that is simple (i.e., no repeated vertices or edges) and has has minimum weight No polynomial-time algorithms are known for computing traveling salesperson tours The traveling salesperson problem (TSP) is a major open problem in computer science
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
Example of traveling salesperson tour (with weight 17)
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