Professional Documents
Culture Documents
Graphs
Graphs
Graphs
43 7
802
SFO
337
1843
ORD
LAX
1 1233
DFW
Graphs
Graphs ( 12.1)
A graph is a pair (V, E), where
V is a set of nodes, called vertices E is a collection of pairs of vertices, called edges Vertices and edges are positions and store elements A vertex represents an airport and stores the three-letter airport code An edge represents a flight route between two airports and stores the mileage of the route 849 PVD
Example:
802
SFO
337
1843
ORD
14
HNL
2555
3 74 1
1233
LAX
8 13
LGA
10
DFW
1120
99
MIA
2
Graphs
Edge Types
Directed edge
ordered pair of vertices (u,v) first vertex u is the origin second vertex v is the destination e.g., a flight unordered pair of vertices (u,v) e.g., a flight route all the edges are directed e.g., route network all the edges are undirected e.g., flight network
Graphs
Undirected edge
ORD
PVD
Directed graph
Undirected graph
Applications
cslab1a cslab1b math.brown.edu
Electronic circuits
Transportation networks
cs.brown.edu
Computer networks
Databases
Entity-relationship 2004 Goodrich, Tamassia
Graphs
Terminology
End vertices (or endpoints) of an edge
U and V are the endpoints of a a, d, and b are incident on V U and V are adjacent X has degree 5 h and i are parallel edges j is a self-loop
Graphs
a U c
V d
b X e
h Z i g
Adjacent vertices
Degree of a vertex
W f
Parallel edges
Self-loop
Terminology (cont.)
Path
sequence of alternating vertices and edges begins with a vertex ends with a vertex each edge is preceded and followed by its endpoints path such that all its vertices and edges are distinct P1=(V,b,X,h,Z) is a simple path P2=(U,c,W,e,X,g,Y,f,W,d,V) is a path that is not simple
Graphs
a U c
V d P2 W f
P1 X g Y
Simple path
Examples
Terminology (cont.)
Cycle
circular sequence of alternating vertices and edges each edge is preceded and followed by its endpoints cycle such that all its vertices and edges are distinct
a U c
V d C2 W f
b X e h C1 g Y Z
Simple cycle
Examples
C1=(V,b,X,g,Y,f,W,c,U,a,) is a simple cycle C =(U,c,W,e,X,g,Y,f,W,d,V,a, 2 ) is a cycle that is not simple Graphs 2004 Goodrich, Tamassia
Properties
Property 1
v deg(v) = 2m
Proof: each edge is counted twice
Notation
n m deg(v) number of vertices number of edges degree of vertex vExample
Property 2
In an undirected graph with no self-loops and no multiple edges m n (n 1)/2 Proof: each vertex has degree at most (n 1)
n= 4 m= 6 deg(v) = 3
Graphs
Update methods
are positions store elements endVertices(e): an array of the two endvertices of e opposite(v, e): the vertex opposite of v on e areAdjacent(v, w): true iff v and w are adjacent replace(v, x): replace element at vertex v with x replace(e, x): replace element at edge e with x
Graphs
Accessor methods
insertVertex(o): insert a vertex storing element o insertEdge(v, w, o): insert an edge (v,w) storing element o removeVertex(v): remove vertex v (and its incident edges) removeEdge(e): remove edge e incidentEdges(v): edges incident to v vertices(): all vertices in the graph edges(): all edges in the graph
9
Iterator methods
Structure ( 12.2.1)
Vertex object
u a v b c w d z
element reference to position in vertex sequence element origin vertex object destination vertex object reference to position in edge sequence sequence of vertex objects
Edge object
Vertex sequence
Edge sequence
sequence of edge 2004 Goodrich,objects Tamassia
Graphs
10
a u
b w
11
a u
b w
Reference to edge object for adjacent vertices Null for non nonadjacent vertices
The old fashioned version just has 0 for no edge and 1 for 2004 Goodrich, Tamassia edge
Graphs
12
Asymptotic Performance
n vertices, m edges no parallel edges no self-loops Bounds are bigOh
Adjacenc y Matrix n2 n 1 n2 1 n2 1
13
Space
Depth-First Search
A B C D E
Graphs
14
Subgraphs
A subgraph S of a graph G is a graph such that
The vertices of S are a subset of the vertices of G The edges of S are a subset of the edges of G
Subgraph
Spanning subgraph
15
Connectivity
A graph is connected if there is a path between every pair of vertices A connected component of a graph G is a maximal connected subgraph of G
2004 Goodrich, Tamassia Graphs
Connected graph
Tree
A forest is an undirected graph without cycles The connected components of a forest are trees
2004 Goodrich, Tamassia Graphs
Forest
17
Graph
Spanning tree
18
Visits all the vertices and edges of G Determines whether G is connected Computes the connected components of G Computes a spanning forest of G
DFS on a graph with n vertices and m edges takes O(n + m ) time DFS can be further extended to solve other graph problems
Find and report a path between two given vertices Find a cycle in the graph
Graphs
DFS Algorithm
The algorithm uses a mechanism for setting and getting labels of vertices and edges Algorithm DFS(G) Input graph G Output labeling of the edges of G as discovery edges and back edges for all u G.vertices() setLabel(u, UNEXPLORED) for all e G.edges() setLabel(e, UNEXPLORED) for all v G.vertices() if getLabel(v) = UNEXPLORED DFS(G, v)
2004 Goodrich, Tamassia
Algorithm DFS(G, v) Input graph G and a start vertex v of G Output labeling of the edges of G in the connected component of v as discovery edges and back edges setLabel(v, VISITED) for all e G.incidentEdges(v) if getLabel(e) = UNEXPLORED w opposite(v,e) if getLabel(w) = UNEXPLORED setLabel(e, DISCOVERY) DFS(G, w) else setLabel(e, BACK)
20
Graphs
Example
A A
unexplored vertex visited vertex unexplored edge discovery edge back edge
A
A B C D E
A D E B C
Graphs 21
B C
2004 Goodrich, Tamassia
Example (cont.)
A B C D E B C A D E
A B C
2004 Goodrich, Tamassia Graphs
A D E B C
22
Properties of DFS
Property 1
DFS(G, v) visits all the vertices and edges in the connected component of v
A
Property 2
The discovery edges labeled by DFS(G, v) form a spanning tree of the connected component of v
2004 Goodrich, Tamassia
Graphs
23
Analysis of DFS
Setting/getting a vertex/edge label takes O(1) time Each vertex is labeled twice
Method incidentEdges is called once for each vertex DFS runs in O(n + m) time provided the graph is represented by the adjacency list structure
Recall that
v deg(v) = 2m
Graphs
24
Path Finding
We can specialize the Algorithm pathDFS(G, v, z) DFS algorithm to find a setLabel(v, VISITED) path between two S.push(v) given vertices u and z if v = z using the template return S.elements() method pattern for all e G.incidentEdges(v) We call DFS(G, u) with u if getLabel(e) = UNEXPLORED as the start vertex w opposite(v,e) We use a stack S to if getLabel(w) = UNEXPLORED keep track of the path setLabel(e, DISCOVERY) between the start S.push(e) vertex and the current pathDFS(G, w, z) vertex S.pop(e) As soon as destination else vertex z is setLabel(e, BACK) encountered, we S.pop(v) return the path as the Graphs 25 2004 Goodrich, Tamassia contents of the stack
Cycle Finding
We can specialize the DFS algorithm to find a simple cycle using the template method pattern We use a stack S to keep track of the path between the start vertex and the current vertex As soon as a back edge (v, w) is encountered, we return the cycle as the portion of the 2004 Goodrich, Tamassia stack from the top to
Algorithm cycleDFS(G, v, z) setLabel(v, VISITED) S.push(v) for all e G.incidentEdges(v) if getLabel(e) = UNEXPLORED w opposite(v,e) S.push(e) if getLabel(w) = UNEXPLORED setLabel(e, DISCOVERY) pathDFS(G, w, z) S.pop(e) else T new empty stack repeat o S.pop() T.push(o) until o = w return T.elements() S.pop(v)
Graphs 26
Breadth-First Search
L0 L1
A C E F D
L2
Graphs
27
Visits all the vertices and edges of G Determines whether G is connected Computes the connected components of G Computes a spanning forest of G
BFS on a graph with n vertices and m edges takes O(n + m ) time BFS can be further extended to solve other graph problems
Graphs
Find and report a path with the minimum number of edges between two given vertices Find a simple cycle,28 if
BFS Algorithm
The algorithm uses a mechanism for setting and getting labels of vertices and edges Algorithm BFS(G) Input graph G Output labeling of the edges and partition of the vertices of G for all u G.vertices() setLabel(u, UNEXPLORED) for all e G.edges() setLabel(e, UNEXPLORED) for all v G.vertices() if getLabel(v) = UNEXPLORED BFS(G, v)
2004 Goodrich, Tamassia
Graphs
Algorithm BFS(G, s) L0 new empty sequence L0.insertLast(s) setLabel(s, VISITED) i0 while Li.isEmpty() Li +1 new empty sequence for all v Li.elements() for all e G.incidentEdges(v) if getLabel(e) = UNEXPLORED w opposite(v,e) if getLabel(w) = UNEXPLORED setLabel(e, DISCOVERY) setLabel(w, VISITED) Li +1.insertLast(w) else setLabel(e, CROSS) i i +1
29
Example
A A
L0 L1
unexplored vertex visited vertex unexplored edge discovery edge cross edge
L0
A C E F
Graphs
A C E F D
L0 L1
A C E F
30
L1
Example (cont.)
L0 L1
A C E F D
L0 L1
A C E F D
L2
L0 L1
A C E F
Graphs
L0 L1
A C E F
31
L2
L2
Example (cont.)
L0 L1
A C E F D
L0 L1
A C E F D
L2
L2
L0 L1
A C E F
Graphs 32
L2
Properties
Notation
Gs: connected component of s
A B E C F D
Property 1
BFS(G, s) visits all the vertices and edges of Gs
Property 2
The discovery edges labeled by BFS(G, s) form a spanning tree Ts of Gs L0
A C E F
33
Property 3
For each vertex v in Li
L1
The path of Ts from s to v has i edges Every path from s to v in Gs has at least i edges
Graphs
L2
Analysis
Setting/getting a vertex/edge label takes O(1) time Each vertex is labeled twice
Each vertex is inserted once into a sequence Li Method incidentEdges is called once for each vertex BFS runs in O(n + m) time provided the graph is represented by the adjacency list structure
2004 Goodrich, Tamassia
Recall that
deg(v) = 2m
Graphs
34
Applications
Using the template method pattern, we can specialize the BFS traversal of a graph G to solve the following problems in O(n + m) time
Compute the connected components of G Compute a spanning forest of G Find a simple cycle in G, or report that G is a forest Given two vertices of G, find a path in G between them with the minimum number of edges, or report that no such path exists
Graphs 35
DFS BFS
L0 L1
A C E F D
L2
DFS
2004 Goodrich, Tamassia Graphs
BFS
36
w is in the same level as v or in the next level in the tree of discovery edges
A C E F D
A B E C F D
L0 L1
L2
DFS
2004 Goodrich, Tamassia Graphs
BFS
37
Directed Graphs
ORD JFK SFO
BOS
LAX
DFW
MIA
Graphs
38
Digraphs ( 12.4)
A digraph is a graph whose edges are all directed
E D C B A
Applications
39
Digraph Properties
C
B A
If G is simple, m < n*(n-1). If we keep in-edges and out-edges in separate adjacency lists, we can perform listing of in-edges and out-edges in time proportional to their size.
2004 Goodrich, Tamassia Graphs 40
Digraph Application
Scheduling: edge (a,b) means task a must be completed before b can be started
ics21 ics22 ics23
ics51
ics53
ics52 ics161
ics131
ics141
ics121
ics171
ics151
2004 Goodrich, Tamassia Graphs
41
Directed DFS
We can specialize the traversal algorithms (DFS and BFS) to digraphs by traversing edges only along their direction In the directed DFS algorithm, we have four types of edges
E D C B A
42
Reachability
DFS tree rooted at v: vertices reachable from v via directed paths
E D C
E C A
D F B
A A E
D C B
43
Graphs
Strong Connectivity
Each vertex can reach all other vertices
a c d e f
2004 Goodrich, Tamassia Graphs
b
44
Connectivity Algorithm
Pick a vertex v in G. Perform a DFS from v in G.
G:
a c d g
e b
G:
d
e b
45
e b
{f,d,e,b}
46
Transitive Closure
Given a digraph G, the transitive closure of G is the digraph G* such that G* has the same vertices as G if G has a directed path from u to v (u v), G* has a directed edge from u to v The transitive closure provides reachability information about a digraph
2004 Goodrich, Tamassia Graphs
D B C A D B C A
E G
G*
47
If there's a way to get from A to B and from B to C, then there's a way to get from A to C.
O(n(n+m))
Idea #1: Number the vertices 1, 2, , n. Idea #2: Consider paths that use only vertices numbered 1, 2, , k, Uses only vertices numbered 1,,k as intermediate vertices:
i Uses only vertices numbered 1,,k-1 k
2004 Goodrich, Tamassia Graphs
Floyd-Warshalls Algorithm
Algorithm FloydWarshall(G) Input digraph G Output transitive closure G* of G i1 for all v G.vertices() denote v as vi G =G 0 ii+1 G has a directed edge (v , G0 G k i vj) if G has a directed path for k 1 to n do from vi to vj with Gk Gk 1 intermediate vertices in for i 1 to n (i k) do the set {v1 , , vk} for j 1 to n (j i, k) do if Gk 1.areAdjacent(vi, vk) We have that Gn = G* Gk 1.areAdjacent(vk, vj) In phase k, digraph Gk is if Gk.areAdjacent(vi, vj) computed from Gk 1 Gk.insertDirectedEdge(vi, vj , k) 3 Running time: O(n ), return Gn Graphs 50
Floyd-Warshalls algorithm numbers the vertices of G as v1 , , vn and computes a series of digraphs G0, , Gn
is
Floyd-Warshall Example
BOS ORD
v7
v4
JFK
v2
SFO
v6
LAX
DFW
v1
v3
MIA
v5
2004 Goodrich, Tamassia Graphs 51
Floyd-Warshall, Iteration 1 v
BOS ORD
v4
JFK
v2
SFO
v6
LAX
DFW
v1
v3
MIA
v5
2004 Goodrich, Tamassia Graphs 52
Floyd-Warshall, Iteration 2 v
BOS ORD
v4
JFK
v2
SFO
v6
LAX
DFW
v1
v3
MIA
v5
2004 Goodrich, Tamassia Graphs 53
Floyd-Warshall, Iteration 3 v
BOS ORD
v4
JFK
v2
SFO
v6
LAX
DFW
v1
v3
MIA
v5
2004 Goodrich, Tamassia Graphs 54
Floyd-Warshall, Iteration 4 v
BOS ORD
v4
JFK
v2
SFO
v6
LAX
DFW
v1
v3
MIA
v5
2004 Goodrich, Tamassia Graphs 55
Floyd-Warshall, Iteration 5 v
BOS ORD
v4
JFK
v2
SFO
v6
LAX
DFW
v1
v3
MIA
v5
2004 Goodrich, Tamassia Graphs 56
Floyd-Warshall, Iteration 6 v
BOS ORD
v4
JFK
v2
SFO
v6
LAX
DFW
v1
v3
MIA
v5
2004 Goodrich, Tamassia Graphs 57
Floyd-Warshall, Conclusionv
BOS ORD
v4
JFK
v2
SFO
v6
LAX
DFW
v1
v3
MIA
v5
2004 Goodrich, Tamassia Graphs 58
D B C A D B C A v4
DAG G E v5
v3 Topological ordering of G 59
Topological Sorting
Number vertices, so that (u,v) in E implies u < v wake up 1 A typical student day
2 study computer sci. 4 7 play nap 8 write c.s. program eat 3 5 more c.s.
6 work out
Algorithm topologicalDFS(G, v) Input graph G and a start vertex v of G Output labeling of the vertices of G in the connected component of v setLabel(v, VISITED) for all e G.incidentEdges(v) if getLabel(e) = UNEXPLORED w opposite(v,e) if getLabel(w) = UNEXPLORED setLabel(e, DISCOVERY) topologicalDFS(G, w) else {e is a forward or cross edge} Label v with topological number n nn-1
62
Graphs
Graphs
63
9
2004 Goodrich, Tamassia Graphs 64
8 9
2004 Goodrich, Tamassia Graphs 65
7 8 9
2004 Goodrich, Tamassia Graphs 66
6 7 8 9
2004 Goodrich, Tamassia Graphs 67
6 7 8 9
2004 Goodrich, Tamassia Graphs
68
4 6 7 8 9
2004 Goodrich, Tamassia Graphs 69
Shortest Paths
8 B 2 8 2 7 5 E 3 C A 0 4 1 8 F D 5 3 2 9
Graphs
73
In a flight route graph, the weight of an edge represents the distance in miles between the endpoint airports
802
SFO
337
1843
ORD
8 13 7
849
42
PVD
1205
HNL
2555
LAX
1 1233
43 7
LGA
10
DFW
1120
99
MIA
74
Graphs
Length of a path is the sum of the weights of its edges. Shortest path between Providence and Honolulu Internet packet routing Flight reservations Driving directions 1843
Example:
Applications
802
SFO
ORD
8 13 7
849
14
PVD
1205
HNL
2555
3 74 1
1233
LGA
LAX
337
10
DFW
1120
99
75
MIA
Graphs
Property 2:
There is a tree of shortest paths from a start vertex to all the other vertices
Example:
Tree of shortest paths from Providence
802
SFO
337
1843
ORD
8 13 7
849
14
PVD
1205
HNL
2555
3 74 1
1233
LGA
10
LAX
DFW
1120
99
MIA
76
Graphs
We grow a cloud of vertices, beginning with s and eventually covering all the vertices We store with each vertex v a label d(v) representing the distance of v from s in the subgraph consisting of the cloud and its adjacent vertices At each step
Graphs
We add to the cloud the vertex u outside the cloud with the smallest distance label, d(u) 77 We update the labels of the
Edge Relaxation
Consider an edge e = (u,z) such that
d(u) = 50 s u e
u is the vertex most recently added to the cloud z is not in the cloud
10 d(z) = 75
d(u) = 50 u e
10 d(z) = 60
Graphs
78
Example
8 B 2 8 E 2 7 3 C A 0 4 1 F D 5 4 B 2 8 8 2 7 5 E 3 C A 0 4 1 8 F D 5 3 2 9 2 9
8 B 2 8 2 7 5 E 3
A C
4 1 11 F D 5
Graphs
8 3 B 2 7 2 7 5 E 3
A C
4 1 8 F D 5
79
2 9
2 9
Example (cont.)
8 B 2 7 5 E 2 7 3 C A 0 4 1 8 F D 5 8 B 2 7 5 E 2 7 3 C A 0 4 1 8 F D 5 3 3 2 9
2 9
Graphs
80
Dijkstras Algorithm
A priority queue stores the vertices outside the cloud
Locator-based methods
Algorithm DijkstraDistances(G, s) Q new heap-based priority queue for all v G.vertices() if v = s setDistance(v, 0) else setDistance(v, ) l Q.insert(getDistance(v), v) setLocator(v,l) while Q.isEmpty() u Q.removeMin() for all e G.incidentEdges(u) { relax edge e } z G.opposite(u,e) r getDistance(u) + weight(e) if r < getDistance(z) setDistance(z,r) Q.replaceKey(getLocator(z),r)
Graphs 81
Method incidentEdges is called once for each vertex We set/get the distance and locator labels of vertex z O(deg(z)) times Setting/getting a label takes O(1) time Each vertex is inserted once into and removed once from the priority queue, where each insertion or removal takes O(log n) time The key of a vertex in the priority queue is modified at most deg(w) times, where each key change takes O(log n) time
Label operations
Dijkstras algorithm runs in O((n + m) log n) time provided the graph is represented by the adjacency list structure
Recall that
v deg(v) = 2m
82
The running time can also be expressed as O(m log n) Graphs 2004 Goodrich, Tamassia since the graph is connected
Algorithm DijkstraShortestPathsTree(G, s) for all v G.vertices() setParent(v, ) for all e G.incidentEdges(u) { relax edge e } z G.opposite(u,e) r getDistance(u) + weight(e) if r < getDistance(z) setDistance(z,r) setParent(z,e) Q.replaceKey(getLocator(z),r)
Graphs 83
2 9
84
If a node with a negative incident edge were to be added late to the cloud, it could mess up distances for vertices already in the cloud.
8 B 2 7 5 E 6 7 0
A C
4 1 9 F D 5 4
5 -8
How?
Graphs
86
Bellman-Ford Example
Nodes are labeled with their d(v) values
8 -2 -2 7 3 0 -2 5 8 1 -2 7 6 3 -2 9 1 9 4 5
Graphs
0 9 1
4 5 8 -2
8 -2 7 3
0 -2 9 0 -2 7 3 -2 9 1 4 9 1
4 5 4
4 -1 5 -2
4 -1 5
87
Graphs
88
DAG Example
1
8 -2
3
0 7
2
4 1 9 5
5 4
8
3
0 -2 7 3
2
4 -2 9 5
5
-5
-5
3
6 1
6 1
8
3
0 -2 7 1 3
6 2
4 1 9 7
4
8 -2 4 5 -1
3
0 7 0 1 3
6 2
4 1 9 7 4
5 89 4
8 -5
-2
5 -5
-2
-1 5
5 Graphs
(two steps)
MIA
Graphs
90
Subgraph of a graph G containing all the vertices of G Spanning subgraph that is itself a (free) tree Spanning tree of a weighted graph with minimum total edge weight Communications networks Transportation networks
Graphs
ORD
1
10
PIT
6 7 3 5
Spanning tree
DEN
9 4 8
STL
DCA
2
Applications
DFW
ATL
91
Cycle Property
Cycle Property:
Let T be a minimum spanning tree of a weighted graph G Let e be an edge of G that is not in T and C let be the cycle formed by e with T For every edge f of C, weight(f) weight(e) Proof: By contradiction If weight(f) > weight(e) we can get a spanning tree of smaller weight by replacing e with f
8 4 9 3 e 7
C
2 6 8 7
Graphs
Partition Property
Partition Property:
Consider a partition of the vertices of G into subsets U and V Let e be an edge of minimum weight across the partition There is a minimum spanning tree of G containing edge e Proof: Let T be an MST of G If T does not contain e, consider the cycle C formed by e with T and let f be an edge of C across the partition By the cycle property, weight(f) weight(e) Thus, weight(f) = weight(e) We obtain another MST by Graphs 2004 Goodrich, Tamassia replacing f with e
f 5 8
7 9 8 e 7
V 4
7 9 8 e
V 4
We are left with one cloud that encompasses the MST A tree T which is our MST
Algorithm KruskalMST(G) for each vertex V in G do define a Cloud(v) of {v} let Q be a priority queue. Insert all edges into Q using their weights as the key T while T has fewer than n-1 edges do edge e = T.removeMin() Let u, v be the endpoints of e if Cloud(v) Cloud(u) then Add edge e to T Merge Cloud(v) and Cloud(u) return T
Graphs
94
Graphs
95
Representation of a Partition
Each set is stored in a sequence Each element has a reference back to the set
operation find(u) takes O(1) time, and returns the set of which u is a member. in operation union(u,v), we move the elements of the smaller set to the sequence of the larger set and update their references the time for operation union(u,v) is min(nu,nv), where nu and nv are the sizes of the sets storing u and v
Whenever an element is processed, it goes into a set of size at least double, hence each Graphs element is 2004 Goodrich, Tamassia processed at most log n times
96
Partition-Based Implementation
A partition-based version of Kruskals Algorithm performs cloud merges as unions and tests as finds. Algorithm Kruskal(G):
Input: A weighted graph G. Output: An MST T for G. Let P be a partition of the vertices of G, where each vertex forms a separate set. Let Q be a priority queue storing the edges of G, sorted by their weights Let T be an initially-empty tree while Q is not empty do (u,v) Q.removeMinElement() if P.find(u) != P.find(v) then Running time: Add (u,v) to T O((n+m)log n) P.union(u,v) return T
2004 Goodrich, Tamassia Graphs 97
Kruskal Example
1846 SFO 337 LAX 1464 1235
2704
867 849 ORD 740 621 187 JFK 184 1391 BWI
802
MIA
Graphs
98
Example
1846 SFO 337 LAX 1464 1235
2704
867 849 ORD 740 621 187 JFK 184 1391 BWI
802
MIA
Graphs
99
Example
1846 SFO 337 LAX 1464 1235
2704
867 849 ORD 740 621 187 JFK 184 1391 BWI
802
MIA
Graphs
100
Example
1846 SFO 337 LAX 1464 1235
2704
867 849 ORD 740 621 187 JFK 184 1391 BWI
802
MIA
Graphs
101
Example
1846 SFO 337 LAX 1464 1235
2704
867 849 ORD 740 621 187 JFK 184 1391 BWI
802
MIA
Graphs
102
Example
1846 SFO 337 LAX 1464 1235
2704
867 849 ORD 740 621 187 JFK 184 1391 BWI
802
MIA
Graphs
103
Example
1846 SFO 337 LAX 1464 1235
2704
867 849 ORD 740 621 187 JFK 184 1391 BWI
802
MIA
Graphs
104
Example
1846 SFO 337 LAX 1464 1235
2704
867 849 ORD 740 621 187 JFK 184 1391 BWI
802
MIA
Graphs
105
Example
1846 SFO 337 LAX 1464 1235
2704
867 849 ORD 740 621 187 JFK 184 1391 BWI
802
MIA
Graphs
106
Example
1846 SFO 337 LAX 1464 1235
2704
867 849 ORD 740 621 187 JFK 184 1391 BWI
802
MIA
Graphs
107
Example
1846 SFO 337 LAX 1464 1235
2704
867 849 ORD 740 621 187 JFK 184 1391 BWI
802
MIA
Graphs
108
Example
1846 SFO 337 LAX 1464 1235
2704
867 849 ORD 740 621 187 JFK 184 1391 BWI
802
MIA
Graphs
109
Example
1846 SFO 337 LAX 1464 1235
2704
867 849 ORD 740 621 187 JFK 184 1391 BWI
802
MIA
Graphs
110
Example
1846 SFO 337 LAX 1464 1235
2704
867 849 ORD 740 621 187 JFK 184 1391 BWI
802
MIA
111
Graphs
Graphs
112
Locator-based methods
Algorithm PrimJarnikMST(G) Q new heap-based priority queue s a vertex of G for all v G.vertices() if v = s setDistance(v, 0) else setDistance(v, ) setParent(v, ) l Q.insert(getDistance(v), v) setLocator(v,l) while Q.isEmpty() u Q.removeMin() for all e G.incidentEdges(u) z G.opposite(u,e) r weight(e) if r < getDistance(z) setDistance(z,r) setParent(z,e) Q.replaceKey(getLocator(z),r)
Graphs 113
Example
2 2 A 7 B 5 8 0 7 7 B 5 8 0 A 7 5 C 9 8 E 7
Graphs
D 9 8
4 F 2 2 A B 5 8 0 5 C
7 9 8
7 4 F
8 C
3 7 7 4 F 3 0 A 2 2
E 7 D 9 8 E 7
3 7
2 2
7 B 5 8 5 C
7 4 F 3 7 4
114
Example (contd.)
2 2 A 7 B 5 8 0 7 5 C 9 8 E 3 2 2 A B 5 8 0
Graphs
7 4 F 3 7 5 C 7 9 8 E 3 D 7 4 F 3 4 4
115
Analysis
Graph operations
Method incidentEdges is called once for each vertex We set/get the distance, parent and locator labels of vertex z O(deg(z)) times Setting/getting a label takes O(1) time Each vertex is inserted once into and removed once from the priority queue, where each insertion or removal takes O(log n) time The key of a vertex w in the priority queue is modified at most deg(w) times, where each key change takes O(log n) time
Label operations
Prim-Jarniks algorithm runs in O((n + m) log n) time provided the graph is represented by the adjacency list structure
Recall that
v deg(v) = 2m
Graphs 116
117
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 Graphs 2004 Goodrich,tour Tamassia
118