Program Graph
Program Graph
Goals
Learn and implement the adjacency matrix structure an
Campus Tour
Kruskal’s minimum spanning tree algorithm
Understand and use the decorator pattern and various JDSL
classes and interfaces
Your task
Implement the adjacency matrix structure for representing a
graph
Implement Kruskal’s MST algorithm
Frontend
Computation and visualization of an approximate traveling
salesperson tour
© 2004 Goodrich, Tamassia Campus Tour 1 © 2004 Goodrich, Tamassia Campus Tour 2
© 2004 Goodrich, Tamassia Campus Tour 3 © 2004 Goodrich, Tamassia Campus Tour 4
Example Example (contd.)
G G G G
8 8 B 8 B 8
B 4 B 4 4 4
E E 9 E 9 E
9 6 9 6 5 6 6
1 5 F 5 F 1 F 1 5 F
C 11 3 1 C 11 3 C 11 3
C 11 3
2 2 2 2
7 H 7 H 7 D H 7 H
D D D
A A A 10 A
10 10 10
four steps
G G G G
8 8 B 8 B 8
B 4 B 4 4 4
E E 9 E 9 E
9 6 9 6 5 6 5 6
1 5 F 5 F 1 F 1 F
1 C 11 3 C 11 3
C 11 3 C 11 3
2 2 2 2
7 H 7 H 7 D H 7 D H
D D A A
A 10 A 10 10
10
© 2004 Goodrich, Tamassia Campus Tour 5 © 2004 Goodrich, Tamassia Campus Tour 6
Worst-case running times The total time for the series Kruskal’s algorithm runs in time O((n + m) log n) time
makeSet, find: O(1) of operations is O(k + n log n) provided the graph has no parallel edges and is
union: O(min(nA, nB)) represented by the adjacency list structure
© 2004 Goodrich, Tamassia Campus Tour 7 © 2004 Goodrich, Tamassia Campus Tour 8
Decorator Pattern Traveling Salesperson Problem
Labels are commonly used in The decorator pattern extends A tour of a graph is a spanning cycle
graph algorithms the methods of the Position (e.g., a cycle that goes through all
Auxiliary data
ADT to support the handling the vertices) 7 D
of attributes (labels) A traveling salesperson tour of a B 4
Output
has(a): tests whether the weighted graph is a tour that is 2
Examples position has attribute a 2 5 F
simple (i.e., no repeated vertices or C
DFS: unexplored/visited get(a): returns the value of edges) and has has minimum weight 8
6 3
label for vertices and attribute a
No polynomial-time algorithms are E
unexplored/ forward/back set(a, x): sets to x the value of A 1
known for computing traveling
labels for edges attribute a
salesperson tours
Dijkstra and Prim-Jarnik: destroy(a): removes attribute Example of traveling
a and its associated value (for The traveling salesperson problem
distance, locator, and (TSP) is a major open problem in salesperson tour
cleanup purposes)
parent labels for vertices computer science
Kruskal: locator label for
The decorator pattern can be (with weight 17)
implemented by storing a Find a polynomial-time algorithm
vertices and MSF label for computing a traveling salesperson
edges
dictionary of (attribute, value) tour or prove that none exists
items at each position
© 2004 Goodrich, Tamassia Campus Tour 9 © 2004 Goodrich, Tamassia Campus Tour 10
TSP Approximation
We can approximate a TSP tour
with a tour of at most twice the
weight for the case of Euclidean
graphs
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
Approximation algorithm
Compute a minimum spanning tree
Form an Eulerian circuit around the
MST
Transform the circuit into a tour