Campus Tour
6/8/2002 2:07 PM
Outline and Reading
Overview of the assignment
Review
Campus Tour
Adjacency matrix structure (§6.2.3)
Kruskal’s 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
1
Graph Assignment
6/8/2002 2:07 PM
Campus Tour
3
Kruskal’s Algorithm
The vertices are
partitioned into clouds
We start with one cloud
per vertex
Clouds are merged during
the execution of the
algorithm
Partition ADT:
Reference to edge
object for adjacent
vertices
Null for non
nonadjacent
vertices
Computation and visualization of an approximate traveling
salesperson tour
6/8/2002 2:07 PM
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
6/8/2002 2:07 PM
w
u
1
0
1
∅
∅
1
6/8/2002 2:07 PM
w
2
∅
0
a
2
v
∅
2
b
∅
Campus Tour
4
Example
Algorithm KruskalMSF(G)
Input weighted graph G
Output labeling of the edges of a
minimum spanning forest of G
5
5
1
6
H
9
5
1
2
C 11
7
A
9
C 11
7
10
6/8/2002 2:07 PM
E
6
F
3
D
H
1
2
A
Campus Tour
6
5
H
C 11
10
2
G
9
7
F
3
8
B
4
4
E
D
10
G
8
5
F
3
D
10
B
A
9
G
8
B
4
E
C 11
7
A
1
G
8
B
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)
Campus Tour
0
2D-array adjacency
array
Frontend
b
u
Integer key (index)
associated with
vertex
Implement the adjacency matrix structure for representing a
graph
Implement Kruskal’s MST algorithm
v
a
Edge list structure
Augmented vertex
objects
Learn and implement the adjacency matrix structure an
Kruskal’s minimum spanning tree algorithm
Understand and use the decorator pattern and various JDSL
classes and interfaces
Your task
2
Adjacency Matrix Structure
Goals
Campus Tour
4
E
6
F
3
D
H
2
6
1
Campus Tour
6/8/2002 2:07 PM
Example (contd.)
8
B
5
1
9
6
H
D
5
1
2
C 11
7
A
9
5
C 11
7
A
10
E
6
F
3
D
H
5
1
4
E
9
C 11
7
A
G
8
B
2
6/8/2002 2:07 PM
2
H
st
e
4
F
3
four steps
tw
o
8
B
1
G
6
D
10
Partition implementation
4
E
9
ps
10
F
3
G
8
B
4
E
C 11
7
A
Partition Implementation
G
6
2
H
D
10
F
3
7
We perform n makeSet operations, 2m find operations and no
more than n − 1 union operations
Label operations
We set vertex labels n times and get them 2m times
Kruskal’s 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
Find a polynomial-time algorithm
computing a traveling salesperson
tour or prove that none exists
6/8/2002 2:07 PM
Campus Tour
2
5
C
F
8
6
A
1
8
The decorator pattern extends
the methods of the Position
ADT to support the handling
of attributes (labels)
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
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
Campus Tour
10
We can approximate a TSP tour
with a tour of at most twice the
weight for the case of Euclidean
graphs
4
2
TSP Approximation
D
7
B
Auxiliary data
Output
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
Examples
We perform m insert operations and m removeMin operations
Partition operations
Campus Tour
Labels are commonly used in
graph algorithms
Methods vertices and edges are called once
Method endVertices is called m times
Priority queue operations
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)
Decorator Pattern
Graph operations
makeSet, find: O(1)
union: O(min(nA, nB))
6/8/2002 2:07 PM
Analysis of Kruskal’s Algorithm
Amortized analysis
Worst-case running times
Campus Tour
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
3
E
Example of traveling
salesperson tour
(with weight 17)
Approximation algorithm
11
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
6/8/2002 2:07 PM
Campus Tour
12
2
© Copyright 2026 Paperzz