0% found this document useful (0 votes)
3 views3 pages

Program Graph

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views3 pages

Program Graph

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Graph Assignment

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

Adjacency Matrix Structure Kruskal’s Algorithm


Edge list structure a v b The vertices are Algorithm KruskalMSF(G)
Input weighted graph G
Augmented vertex u w partitioned into clouds Output labeling of the edges of a
objects  We start with one cloud minimum spanning forest of G
 Integer key (index) per vertex Q ← new heap-based priority queue
associated with  Clouds are merged during for all v ∈ G.vertices() do
vertex the execution of the l ← makeSet(v) { elementary cloud }
2D-array adjacency 0 u 1 v 2 w algorithm v.setLocator(l)
for all e ∈ G.edges() do
array Partition ADT: Q.insert(e.weight(), e)
 Reference to edge while ¬Q.empty()
0 1 2 makeSet(o): create set {o}
object for adjacent 
e ← Q.removeMin()
vertices 0 ∅ ∅ and return a locator for [u,v] ← e.endVertices()
Null for non object o A ← find(u.getLocator())

1 ∅ B ← find(v.getLocator())
nonadjacent  find(l): return the set of
vertices a 2 ∅ ∅ b the object with locator l if A ≠ B
setMSFedge(e)
 union(A,B): merge sets A { merge clouds }
and B union(A, B)

© 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

Partition Implementation Analysis of Kruskal’s Algorithm


Partition implementation Amortized analysis Graph operations
 A set is represented the  Consider a series of k Partiton  Methods vertices and edges are called once
sequence of its elements ADT operations that includes  Method endVertices is called m times
A position stores a reference n makeSet operations
Priority queue operations

back to the sequence itself (for
operation find) Each time we move an
We perform m insert operations and m removeMin operations


element into a new sequence,
The position of an element in

the sequence serves as locator the size of its set at least Partition operations
for the element in the set doubles  We perform n makeSet operations, 2m find operations and no
 In operation union, we move  An element is moved at most more than n − 1 union operations
the elements of the smaller log2 n times
sequence into to the larger
Label operations
Moving an element takes O(1)
We set vertex labels n times and get them 2m times

sequence time

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

© 2004 Goodrich, Tamassia Campus Tour 11

You might also like