1 Introduction to Graph Data Structure Applications Graph Searching Minimum Spanning Trees ...
-
Upload
ophelia-sparks -
Category
Documents
-
view
224 -
download
3
Transcript of 1 Introduction to Graph Data Structure Applications Graph Searching Minimum Spanning Trees ...
![Page 1: 1 Introduction to Graph Data Structure Applications Graph Searching Minimum Spanning Trees Shortest Path problems.](https://reader035.fdocuments.us/reader035/viewer/2022062801/56649e315503460f94b2235d/html5/thumbnails/1.jpg)
1
Introduction to Graph Data Structure
Applications
Graph Searching
Minimum Spanning Trees
Shortest Path problems
![Page 2: 1 Introduction to Graph Data Structure Applications Graph Searching Minimum Spanning Trees Shortest Path problems.](https://reader035.fdocuments.us/reader035/viewer/2022062801/56649e315503460f94b2235d/html5/thumbnails/2.jpg)
2
Graph Definitions A graph G is denoted by G = (V, E) where
◦ V is the set of vertices or nodes of the graph◦ E is the set of edges or arcs connecting the vertices in V
Each edge E is denoted as a pair (v,w) where v,w e V
For example in the graph below
1
2
5
6 4
3
V = {1, 2, 3, 4, 5, 6}E = {(1, 2) (2, 5) (3, 6) (4, 6) (5, 6)}● This is an example of an
unordered or undirected graph
![Page 3: 1 Introduction to Graph Data Structure Applications Graph Searching Minimum Spanning Trees Shortest Path problems.](https://reader035.fdocuments.us/reader035/viewer/2022062801/56649e315503460f94b2235d/html5/thumbnails/3.jpg)
3
Graph Definitions
Complete Graph◦ A graph is complete, if every possible edge exists
![Page 4: 1 Introduction to Graph Data Structure Applications Graph Searching Minimum Spanning Trees Shortest Path problems.](https://reader035.fdocuments.us/reader035/viewer/2022062801/56649e315503460f94b2235d/html5/thumbnails/4.jpg)
4
Graph Definitions (contd.) If the pair of vertices is ordered then the
graph is a directed graph or a di-graph
1
2
5
6 4
3 Here, V = {1, 2, 3, 4, 5, 6}E = {(1, 2) (2, 5) (5, 6) (6, 3) (6, 4)}
● Vertex v is adjacent to w iff (v,w) e E● Sometimes an edge has another component called a
weight or cost. If the weight is absent it is assumed to be 1
![Page 5: 1 Introduction to Graph Data Structure Applications Graph Searching Minimum Spanning Trees Shortest Path problems.](https://reader035.fdocuments.us/reader035/viewer/2022062801/56649e315503460f94b2235d/html5/thumbnails/5.jpg)
5
Graph Definitions (contd.)
1
2
5
6 4
3
1.2
2
1
2.5
4
Here, V = {1, 2, 3, 4, 5, 6}E = {(1, 2,4) (2, 5,1) (5,
6,1.2) (6, 3,2.5) (6, 4,2)}
![Page 6: 1 Introduction to Graph Data Structure Applications Graph Searching Minimum Spanning Trees Shortest Path problems.](https://reader035.fdocuments.us/reader035/viewer/2022062801/56649e315503460f94b2235d/html5/thumbnails/6.jpg)
6
Path
A path of length k is a sequence v , v , …, v of vertices such that (v , v ) for i = 0, 1, …, k – 1 is an edge of G.
0 1 k
i i+1
g
a
e
j
n
b
f
k
dc
o
h
l
p
m
q
Non-simple path:
a, b, e, f, g, b, g, l
Simple path:a, e, k, p, l, qm, h, d, c, g
(no repeatedvertices)
b, c, d not a path
g
a
e
j
n
b
f
k
dc
o
h
l
p
m
q
g
a
e
j
n
b
f
k
dc
o
h
l
p
m
q
ff
b
f ge
j
n
f
k
d
o
h
l
p
m
q
fff
c
ge
j
n
f
k
d
o
h
l
p
m
q
fff
b c
ge
j
n
f
k
d
o
h
l
p
m
q
fff
a b c
ge
j
n
f
k
d
o
h
l
p
m
q
fff
![Page 7: 1 Introduction to Graph Data Structure Applications Graph Searching Minimum Spanning Trees Shortest Path problems.](https://reader035.fdocuments.us/reader035/viewer/2022062801/56649e315503460f94b2235d/html5/thumbnails/7.jpg)
7
Cycle
a
e
j
n
b
f
k
dc
o
g h
l
p
m
q
A cycle is a path that starts and ends at the same vertex.
A simple cycle has no repeated vertices.
k, j, n, k, p, o,k is not simple.
jj
n
j
n
j
n
j
n
j
on
j
![Page 8: 1 Introduction to Graph Data Structure Applications Graph Searching Minimum Spanning Trees Shortest Path problems.](https://reader035.fdocuments.us/reader035/viewer/2022062801/56649e315503460f94b2235d/html5/thumbnails/8.jpg)
8
Subgraph
a
e
j
n
b
f
k
dc
o
g h
l
p
m
q
A subgraph H of G
is a graph; its edges and vertices are subsets of those of G.
V(H) = {b, d, e, f, g, h, l, p, q} E(H) = {(b, e), (b, g), (e, f), (d, h), (l, p), (l, q)}
![Page 9: 1 Introduction to Graph Data Structure Applications Graph Searching Minimum Spanning Trees Shortest Path problems.](https://reader035.fdocuments.us/reader035/viewer/2022062801/56649e315503460f94b2235d/html5/thumbnails/9.jpg)
9
ConnectivityG is connected if there is a path between every pair of vertices.
a
b
d
fe
c
If G is not connected, the maximal connected subgraphs are the connected components of G.
e f ga
cb
dC
CC
1
32
![Page 10: 1 Introduction to Graph Data Structure Applications Graph Searching Minimum Spanning Trees Shortest Path problems.](https://reader035.fdocuments.us/reader035/viewer/2022062801/56649e315503460f94b2235d/html5/thumbnails/10.jpg)
10
Strong & Weak ConnectivityA directed graph is strongly connected if every two vertices are reachable from each other.
a
f
e
cd
b
It is weakly connected if the underlying undirected graph is connected.
f
e
a
d
c
b
![Page 11: 1 Introduction to Graph Data Structure Applications Graph Searching Minimum Spanning Trees Shortest Path problems.](https://reader035.fdocuments.us/reader035/viewer/2022062801/56649e315503460f94b2235d/html5/thumbnails/11.jpg)
Tree is a graph in which any two vertices are connected by exactly one path
or any connected
graph with no cycles is a tree.
A forest is a disjoint union of trees.
11
Tree, Forest
![Page 12: 1 Introduction to Graph Data Structure Applications Graph Searching Minimum Spanning Trees Shortest Path problems.](https://reader035.fdocuments.us/reader035/viewer/2022062801/56649e315503460f94b2235d/html5/thumbnails/12.jpg)
12
Property of ConnectivityLet G = (V, E) be an undirected graph.
If G is connected, then | E | ≥ | V | – 1.
If G is a tree, then | E | = | V | – 1.
If G is a forest, then | E | ≤ | V | – 1.
![Page 13: 1 Introduction to Graph Data Structure Applications Graph Searching Minimum Spanning Trees Shortest Path problems.](https://reader035.fdocuments.us/reader035/viewer/2022062801/56649e315503460f94b2235d/html5/thumbnails/13.jpg)
13
Degree In an undirected graph, the degree of a
vertex is the number of edges that are attached to that vertex.
Each vertex has degree = 3
![Page 14: 1 Introduction to Graph Data Structure Applications Graph Searching Minimum Spanning Trees Shortest Path problems.](https://reader035.fdocuments.us/reader035/viewer/2022062801/56649e315503460f94b2235d/html5/thumbnails/14.jpg)
14
Degree cont.
In a directed graph, we have◦ In-degree: # of edges that end at that vertex◦ Out-degree: # of edges that originate at that
vertex
1
2
5
4
3
2
31
2
3
4
1
2
3
4
1
2
3
6
![Page 15: 1 Introduction to Graph Data Structure Applications Graph Searching Minimum Spanning Trees Shortest Path problems.](https://reader035.fdocuments.us/reader035/viewer/2022062801/56649e315503460f94b2235d/html5/thumbnails/15.jpg)
15
Applications of Graphs Driving Map
◦ Edge = Road◦ Vertex = Intersection ◦ Edge weight = Time required to cover the road
Airline Traffic◦ Vertex = Cities serviced by the airline◦ Edge = Flight exists between two cities◦ Edge weight = Flight time or flight cost or both
Computer networks◦ Vertex = Server nodes◦ Edge = Data link◦ Edge weight = Connection speed
![Page 16: 1 Introduction to Graph Data Structure Applications Graph Searching Minimum Spanning Trees Shortest Path problems.](https://reader035.fdocuments.us/reader035/viewer/2022062801/56649e315503460f94b2235d/html5/thumbnails/16.jpg)
Applications of Graphs
16
Graphs describe relationships
Molecules
Social Networks
Geometric Surfaces (CAD)
Circuits
Parts in an Assembly
…
JohnYoko Ringo
George
Paul
Linda
Flow Charts
![Page 17: 1 Introduction to Graph Data Structure Applications Graph Searching Minimum Spanning Trees Shortest Path problems.](https://reader035.fdocuments.us/reader035/viewer/2022062801/56649e315503460f94b2235d/html5/thumbnails/17.jpg)
17
Representing Graphs: Adjacency Matrix
Adjacency Matrix ◦ Two dimensional matrix of size n x n where n is the number
of vertices in the graph
◦ a[i, j] = 0 if there is no edge between vertices i and j
◦ a[i, j] = 1 if there is an edge between vertices i and j
◦ Undirected graphs have both a[i, j] and a[j, i] = 1 if there is an edge between vertices i and j
For weighted graphs, a[i,j] = weight of edge between vertices i and j
![Page 18: 1 Introduction to Graph Data Structure Applications Graph Searching Minimum Spanning Trees Shortest Path problems.](https://reader035.fdocuments.us/reader035/viewer/2022062801/56649e315503460f94b2235d/html5/thumbnails/18.jpg)
18
Adjacency Matrix
2
1 3
5 4
ijA = (a )
a = ij
1 if (i, j) E(G)
0 otherwise
1 0 1 1 0 12 1 0 1 0 03 1 1 0 1 14 0 0 1 0 15 1 0 1 1 0
1 2 3 4 5
Space: (|V| ). 2
Preferred when the graph is small or dense.
![Page 19: 1 Introduction to Graph Data Structure Applications Graph Searching Minimum Spanning Trees Shortest Path problems.](https://reader035.fdocuments.us/reader035/viewer/2022062801/56649e315503460f94b2235d/html5/thumbnails/19.jpg)
19
Graphs: Adjacency Matrix Example:
1
2 4
3
a
d
b c
A 1 2 3 4
1
2
3 ??4
Symmetric or un-symmetric?
![Page 20: 1 Introduction to Graph Data Structure Applications Graph Searching Minimum Spanning Trees Shortest Path problems.](https://reader035.fdocuments.us/reader035/viewer/2022062801/56649e315503460f94b2235d/html5/thumbnails/20.jpg)
20
Graphs: Adjacency Matrix Example:
1
2 4
3
a
d
b c
A 1 2 3 4
1 0 1 1 0
2 0 0 1 0
3 0 0 0 0
4 0 0 1 0
![Page 21: 1 Introduction to Graph Data Structure Applications Graph Searching Minimum Spanning Trees Shortest Path problems.](https://reader035.fdocuments.us/reader035/viewer/2022062801/56649e315503460f94b2235d/html5/thumbnails/21.jpg)
21
Problem with Adjacency Matrix Space requirement is Q(N2)
Problem: The array is very sparsely populated. For example if a directed graph has 4 vertices and 3 edges, the adjacency matrix has 16 cells only 3 of which are 1
![Page 22: 1 Introduction to Graph Data Structure Applications Graph Searching Minimum Spanning Trees Shortest Path problems.](https://reader035.fdocuments.us/reader035/viewer/2022062801/56649e315503460f94b2235d/html5/thumbnails/22.jpg)
22
Adjacency Lists
2
1 3
5 4
2 3 5
1 3
1 2 4 5
3 5
1 3 4
If G is directed, the total length of all the adjacency lists is | E |.
1
2
3
4
5
Adj
If G is undirected, the total length is 2 | E |.
Space requirement: (|V| + |E|). Preferable representation.
![Page 23: 1 Introduction to Graph Data Structure Applications Graph Searching Minimum Spanning Trees Shortest Path problems.](https://reader035.fdocuments.us/reader035/viewer/2022062801/56649e315503460f94b2235d/html5/thumbnails/23.jpg)
23
Tradeoffs between Adjacency Matrices & Adjacency Lists
Comparison Winner
Faster to test if (x, y) exists ? Matrices
Faster to find vertex degree ? Lists
Less memory on small graphs ? Lists (m+n) vs. Matrices O(n2)
Edge insertion or deletion ? Matrices (1) vs. Lists O(di)
Faster to traverse the graph ? Lists (m+n) vs. Matrices (n2)
Better for most Problems ? Lists
![Page 24: 1 Introduction to Graph Data Structure Applications Graph Searching Minimum Spanning Trees Shortest Path problems.](https://reader035.fdocuments.us/reader035/viewer/2022062801/56649e315503460f94b2235d/html5/thumbnails/24.jpg)
24
Negative Cost Cycle A negative cost cycle is a cycle such that the sum
of the costs of the edges is negative The more we cycle through a negative cost cycle,
the lower the cost of the cycle becomes
2
3
4
4 2
-12
Cost of the path v1-v5
• No traversal of cycle: 6 • One traversal of cycle: 0• Two traversals of cycle: -6• Three traversals of cycle: -12
...
5
1
5
1
• Negative cost cycles are not allowed when we traverse a graph to find the weighted shortest path
![Page 25: 1 Introduction to Graph Data Structure Applications Graph Searching Minimum Spanning Trees Shortest Path problems.](https://reader035.fdocuments.us/reader035/viewer/2022062801/56649e315503460f94b2235d/html5/thumbnails/25.jpg)
25
Using a model to solve a complicated traffic light problem
GIVEN: A complex intersection.
OBJECTIVE: Traffic light with minimum phases.
SOLUTION:• Identify permitted turns, going straight is a “turn”.
• Make group of permitted turns.
• Make the smallest possible number of groups.
• Assign each phase of the traffic light to a group.
![Page 26: 1 Introduction to Graph Data Structure Applications Graph Searching Minimum Spanning Trees Shortest Path problems.](https://reader035.fdocuments.us/reader035/viewer/2022062801/56649e315503460f94b2235d/html5/thumbnails/26.jpg)
26
Using a model to solve a complicated traffic light problem
AB
C
D
E
An intersection
![Page 27: 1 Introduction to Graph Data Structure Applications Graph Searching Minimum Spanning Trees Shortest Path problems.](https://reader035.fdocuments.us/reader035/viewer/2022062801/56649e315503460f94b2235d/html5/thumbnails/27.jpg)
27
Using a model to solve a complicated traffic light problem
Roads C and E are one way, others are two way.
There are 13 permitted turns.
Some turns such as AB (from A to B) and EC can be carried out simultaneously.
Other like AD and EB cross each other and can not be carried out simultaneously.
The traffic light should permit AB and EC simultaneously, but should not allow AD and EB.
![Page 28: 1 Introduction to Graph Data Structure Applications Graph Searching Minimum Spanning Trees Shortest Path problems.](https://reader035.fdocuments.us/reader035/viewer/2022062801/56649e315503460f94b2235d/html5/thumbnails/28.jpg)
28
Using a model to solve a complicated traffic light problem
AB
C
D
E
An intersection
AB & AC
AD & EB
![Page 29: 1 Introduction to Graph Data Structure Applications Graph Searching Minimum Spanning Trees Shortest Path problems.](https://reader035.fdocuments.us/reader035/viewer/2022062801/56649e315503460f94b2235d/html5/thumbnails/29.jpg)
29
Using a model to solve a complicated traffic light problem
SOLUTION:
• We model the problem using a structure called graph G(V,E).
• A graph consists of a set of points called vertices, and lines connecting the points, called edges.
• Drawing a graph such that the vertices represent turns.
• Edges between those turns that can NOT be performed simultaneously.
![Page 30: 1 Introduction to Graph Data Structure Applications Graph Searching Minimum Spanning Trees Shortest Path problems.](https://reader035.fdocuments.us/reader035/viewer/2022062801/56649e315503460f94b2235d/html5/thumbnails/30.jpg)
30
Using a model to solve a complicated traffic light problem
AB
C
D
E
An intersection
AB AC AD
BA BC BD
DA DB DC
EA EB EC ED
Partial graph of incompatible turns
![Page 31: 1 Introduction to Graph Data Structure Applications Graph Searching Minimum Spanning Trees Shortest Path problems.](https://reader035.fdocuments.us/reader035/viewer/2022062801/56649e315503460f94b2235d/html5/thumbnails/31.jpg)
31
Using a model to solve a complicated traffic light problem
Partial table of incompatible turns
![Page 32: 1 Introduction to Graph Data Structure Applications Graph Searching Minimum Spanning Trees Shortest Path problems.](https://reader035.fdocuments.us/reader035/viewer/2022062801/56649e315503460f94b2235d/html5/thumbnails/32.jpg)
32
Using a model to solve a complicated traffic light problem
SOLUTION:• The graph can aid in solving our problem.
• A coloring of a graph is an assignment of a color to each vertex of the graph, so that no two vertices connected by an edge have the same color.
• Our problem is of coloring the graph of incompatible turns using as few colors as possible.
![Page 33: 1 Introduction to Graph Data Structure Applications Graph Searching Minimum Spanning Trees Shortest Path problems.](https://reader035.fdocuments.us/reader035/viewer/2022062801/56649e315503460f94b2235d/html5/thumbnails/33.jpg)
33
Using a model to solve a complicated traffic light problem
GIVEN: A complex intersection.
OBJECTIVE: Traffic light with minimum phases.
SOLUTION:• Identify permitted turns, going straight is a “turn”.
• Make group of permitted turns.
• Make the smallest possible number of groups.
• Assign each phase of the traffic light to a group.
![Page 34: 1 Introduction to Graph Data Structure Applications Graph Searching Minimum Spanning Trees Shortest Path problems.](https://reader035.fdocuments.us/reader035/viewer/2022062801/56649e315503460f94b2235d/html5/thumbnails/34.jpg)
34
Using a model to solve a complicated traffic light problem
AB
C
D
E
An intersection
![Page 35: 1 Introduction to Graph Data Structure Applications Graph Searching Minimum Spanning Trees Shortest Path problems.](https://reader035.fdocuments.us/reader035/viewer/2022062801/56649e315503460f94b2235d/html5/thumbnails/35.jpg)
35
Using a model to solve a complicated traffic light problem
Roads C and E are one way, others are two way.
There are 13 permitted turns.
Some turns such as AB (from A to B) and EC can be carried out simultaneously.
Other like AD and EB cross each other and can not be carried out simultaneously.
The traffic light should permit AB and EC simultaneously, but should not allow AD and EB.
![Page 36: 1 Introduction to Graph Data Structure Applications Graph Searching Minimum Spanning Trees Shortest Path problems.](https://reader035.fdocuments.us/reader035/viewer/2022062801/56649e315503460f94b2235d/html5/thumbnails/36.jpg)
36
Using a model to solve a complicated traffic light problem
AB
C
D
E
An intersection
AB & AC
AD & EB
![Page 37: 1 Introduction to Graph Data Structure Applications Graph Searching Minimum Spanning Trees Shortest Path problems.](https://reader035.fdocuments.us/reader035/viewer/2022062801/56649e315503460f94b2235d/html5/thumbnails/37.jpg)
37
Using a model to solve a complicated traffic light problem
SOLUTION:
• We model the problem using a structure called graph G(V,E).
• A graph consists of a set of points called vertices, and lines connecting the points, called edges.
• Drawing a graph such that the vertices represent turns.
• Edges between those turns that can NOT be performed simultaneously.
![Page 38: 1 Introduction to Graph Data Structure Applications Graph Searching Minimum Spanning Trees Shortest Path problems.](https://reader035.fdocuments.us/reader035/viewer/2022062801/56649e315503460f94b2235d/html5/thumbnails/38.jpg)
38
Using a model to solve a complicated traffic light problem
AB
C
D
E
An intersection
AB AC AD
BA BC BD
DA DB DC
EA EB EC ED
Partial graph of incompatible turns
![Page 39: 1 Introduction to Graph Data Structure Applications Graph Searching Minimum Spanning Trees Shortest Path problems.](https://reader035.fdocuments.us/reader035/viewer/2022062801/56649e315503460f94b2235d/html5/thumbnails/39.jpg)
39
Using a model to solve a complicated traffic light problem
Partial table of incompatible turns
![Page 40: 1 Introduction to Graph Data Structure Applications Graph Searching Minimum Spanning Trees Shortest Path problems.](https://reader035.fdocuments.us/reader035/viewer/2022062801/56649e315503460f94b2235d/html5/thumbnails/40.jpg)
40
Using a model to solve a complicated traffic light problem
SOLUTION:• The graph can aid in solving our problem.
• A coloring of a graph is an assignment of a color to each vertex of the graph, so that no two vertices connected by an edge have the same color.
• Our problem is of coloring the graph of incompatible turns using as few colors as possible.
![Page 41: 1 Introduction to Graph Data Structure Applications Graph Searching Minimum Spanning Trees Shortest Path problems.](https://reader035.fdocuments.us/reader035/viewer/2022062801/56649e315503460f94b2235d/html5/thumbnails/41.jpg)
41
Graphs
Muhammad Amjad Iqbal
![Page 42: 1 Introduction to Graph Data Structure Applications Graph Searching Minimum Spanning Trees Shortest Path problems.](https://reader035.fdocuments.us/reader035/viewer/2022062801/56649e315503460f94b2235d/html5/thumbnails/42.jpg)
42
Introduction to Graph Data Structure
Applications
Graph Searching
Minimum Spanning Trees
Shortest Path problems
![Page 43: 1 Introduction to Graph Data Structure Applications Graph Searching Minimum Spanning Trees Shortest Path problems.](https://reader035.fdocuments.us/reader035/viewer/2022062801/56649e315503460f94b2235d/html5/thumbnails/43.jpg)
43
Graphs Searching
![Page 44: 1 Introduction to Graph Data Structure Applications Graph Searching Minimum Spanning Trees Shortest Path problems.](https://reader035.fdocuments.us/reader035/viewer/2022062801/56649e315503460f94b2235d/html5/thumbnails/44.jpg)
44
Graph Searching
Given: a graph G = (V, E), directed or undirected
Goal: methodically explore every vertex and every edge
Ultimately: build a tree on the graph◦ Pick a vertex as the root◦ Choose certain edges to produce a tree◦ Note: might also build a forest if graph is not
connected
![Page 45: 1 Introduction to Graph Data Structure Applications Graph Searching Minimum Spanning Trees Shortest Path problems.](https://reader035.fdocuments.us/reader035/viewer/2022062801/56649e315503460f94b2235d/html5/thumbnails/45.jpg)
45
Breadth-First Search Given a G=(V,E) and distinguished source vertex s,
BFS systematically explores the edges of G to “discover” every vertex reachable from s.
Creates a BFS tree rooted at s that contains all such vertices.
Expands the frontier between discovered and undiscovered vertices uniformly across the breadth of the frontier.
The algorithm discovers all vertices at distance k from s before discovering any vertices at distance k+1
![Page 46: 1 Introduction to Graph Data Structure Applications Graph Searching Minimum Spanning Trees Shortest Path problems.](https://reader035.fdocuments.us/reader035/viewer/2022062801/56649e315503460f94b2235d/html5/thumbnails/46.jpg)
46
will associate vertex “colors” to guide the algorithm◦ White vertices have not been discovered All vertices start out white
◦ Grey vertices are discovered but not fully explored They may be adjacent to white vertices and represent the
frontier between the discovered and the undiscovered.◦ Black vertices are discovered and fully explored They are adjacent only to black and gray vertices
Explore vertices by scanning adjacency list of grey vertices
Breadth-First Search
![Page 47: 1 Introduction to Graph Data Structure Applications Graph Searching Minimum Spanning Trees Shortest Path problems.](https://reader035.fdocuments.us/reader035/viewer/2022062801/56649e315503460f94b2235d/html5/thumbnails/47.jpg)
47
BFS(G, s) { for each vertex u V [G] - {s} do color[u] ← WHITE u->d ← ∞ u->p ← NIL Q = {s}; // Q is a queue initialize to s while (Q not empty) { u = Dequeue(Q); for each v u->adj { if (v->color == WHITE)
{ v->color = GREY; v->d = u->d + 1; v->p = u; Enqueue(Q, v);
} } u->color = BLACK; }}
Breadth-First Search
What does v->p represent?
What does v->d represent?
![Page 48: 1 Introduction to Graph Data Structure Applications Graph Searching Minimum Spanning Trees Shortest Path problems.](https://reader035.fdocuments.us/reader035/viewer/2022062801/56649e315503460f94b2235d/html5/thumbnails/48.jpg)
48
Breadth-First Search: Example
r s t u
v w x y
![Page 49: 1 Introduction to Graph Data Structure Applications Graph Searching Minimum Spanning Trees Shortest Path problems.](https://reader035.fdocuments.us/reader035/viewer/2022062801/56649e315503460f94b2235d/html5/thumbnails/49.jpg)
49
Breadth-First Search: Example
0
r s t u
v w x y
sQ:
![Page 50: 1 Introduction to Graph Data Structure Applications Graph Searching Minimum Spanning Trees Shortest Path problems.](https://reader035.fdocuments.us/reader035/viewer/2022062801/56649e315503460f94b2235d/html5/thumbnails/50.jpg)
50
Breadth-First Search: Example
1
0
1
r s t u
v w x y
wQ: r
![Page 51: 1 Introduction to Graph Data Structure Applications Graph Searching Minimum Spanning Trees Shortest Path problems.](https://reader035.fdocuments.us/reader035/viewer/2022062801/56649e315503460f94b2235d/html5/thumbnails/51.jpg)
51
Breadth-First Search: Example
1
0
1
2
2
r s t u
v w x y
rQ: t x
![Page 52: 1 Introduction to Graph Data Structure Applications Graph Searching Minimum Spanning Trees Shortest Path problems.](https://reader035.fdocuments.us/reader035/viewer/2022062801/56649e315503460f94b2235d/html5/thumbnails/52.jpg)
52
Breadth-First Search: Example
1
2
0
1
2
2
r s t u
v w x y
Q: t x v
![Page 53: 1 Introduction to Graph Data Structure Applications Graph Searching Minimum Spanning Trees Shortest Path problems.](https://reader035.fdocuments.us/reader035/viewer/2022062801/56649e315503460f94b2235d/html5/thumbnails/53.jpg)
53
Breadth-First Search: Example
1
2
0
1
2
2
3
r s t u
v w x y
Q: x v u
![Page 54: 1 Introduction to Graph Data Structure Applications Graph Searching Minimum Spanning Trees Shortest Path problems.](https://reader035.fdocuments.us/reader035/viewer/2022062801/56649e315503460f94b2235d/html5/thumbnails/54.jpg)
54
Breadth-First Search: Example
1
2
0
1
2
2
3
3
r s t u
v w x y
Q: v u y
![Page 55: 1 Introduction to Graph Data Structure Applications Graph Searching Minimum Spanning Trees Shortest Path problems.](https://reader035.fdocuments.us/reader035/viewer/2022062801/56649e315503460f94b2235d/html5/thumbnails/55.jpg)
55
Breadth-First Search: Example
1
2
0
1
2
2
3
3
r s t u
v w x y
Q: u y
![Page 56: 1 Introduction to Graph Data Structure Applications Graph Searching Minimum Spanning Trees Shortest Path problems.](https://reader035.fdocuments.us/reader035/viewer/2022062801/56649e315503460f94b2235d/html5/thumbnails/56.jpg)
56
Breadth-First Search: Example
1
2
0
1
2
2
3
3
r s t u
v w x y
Q: y
![Page 57: 1 Introduction to Graph Data Structure Applications Graph Searching Minimum Spanning Trees Shortest Path problems.](https://reader035.fdocuments.us/reader035/viewer/2022062801/56649e315503460f94b2235d/html5/thumbnails/57.jpg)
57
Breadth-First Search: Example
1
2
0
1
2
2
3
3
r s t u
v w x y
Q: Ø
![Page 58: 1 Introduction to Graph Data Structure Applications Graph Searching Minimum Spanning Trees Shortest Path problems.](https://reader035.fdocuments.us/reader035/viewer/2022062801/56649e315503460f94b2235d/html5/thumbnails/58.jpg)
58
Breadth-First Search: Properties BFS calculates the shortest-path distance to
the source node◦ Shortest-path distance (s,v) = minimum number
of edges from s to v, or if v not reachable from s BFS builds breadth-first tree, in which paths
to root represent shortest paths in G◦ Thus can use BFS to calculate shortest path from
one vertex to another in O(V+E) time
![Page 59: 1 Introduction to Graph Data Structure Applications Graph Searching Minimum Spanning Trees Shortest Path problems.](https://reader035.fdocuments.us/reader035/viewer/2022062801/56649e315503460f94b2235d/html5/thumbnails/59.jpg)
59
Depth First SearchDepth-first search: StrategyGo as deep as can visiting un-visited nodesChoose any un-visited vertex when you have a
choiceWhen stuck at a dead-end, backtrack as little aspossibleBack up to where you could go to another unvisited
vertexThen continue to go on from that pointEventually you’ll return to where you started
![Page 60: 1 Introduction to Graph Data Structure Applications Graph Searching Minimum Spanning Trees Shortest Path problems.](https://reader035.fdocuments.us/reader035/viewer/2022062801/56649e315503460f94b2235d/html5/thumbnails/60.jpg)
60
DFS AlgorithmDFS(G)
for each vertex u V[G] {color[u]=whiteparent[u]=NULL
}time=0 for each vertex u V[G] {
if color[u]=white thenDFS-VISIT(u)
}
![Page 61: 1 Introduction to Graph Data Structure Applications Graph Searching Minimum Spanning Trees Shortest Path problems.](https://reader035.fdocuments.us/reader035/viewer/2022062801/56649e315503460f94b2235d/html5/thumbnails/61.jpg)
61
DFS-VISIT(u)color[u]=GRAYtime=time+1d[u]=time for each vertex v adj[u] {
if color[v]=white Thenparent[v]=uDFS-VISIT(v)
}color[u]=blackf[u]=time=time+1
![Page 62: 1 Introduction to Graph Data Structure Applications Graph Searching Minimum Spanning Trees Shortest Path problems.](https://reader035.fdocuments.us/reader035/viewer/2022062801/56649e315503460f94b2235d/html5/thumbnails/62.jpg)
62
Depth-First Search: Example
u v w
x y z
![Page 63: 1 Introduction to Graph Data Structure Applications Graph Searching Minimum Spanning Trees Shortest Path problems.](https://reader035.fdocuments.us/reader035/viewer/2022062801/56649e315503460f94b2235d/html5/thumbnails/63.jpg)
63
Depth-First Search: Example
1/
u v w
x y z
![Page 64: 1 Introduction to Graph Data Structure Applications Graph Searching Minimum Spanning Trees Shortest Path problems.](https://reader035.fdocuments.us/reader035/viewer/2022062801/56649e315503460f94b2235d/html5/thumbnails/64.jpg)
64
Depth-First Search: Example
1/ 2/
u v w
x y z
![Page 65: 1 Introduction to Graph Data Structure Applications Graph Searching Minimum Spanning Trees Shortest Path problems.](https://reader035.fdocuments.us/reader035/viewer/2022062801/56649e315503460f94b2235d/html5/thumbnails/65.jpg)
65
Depth-First Search: Example
1/ 2/
3/
u v w
x y z
![Page 66: 1 Introduction to Graph Data Structure Applications Graph Searching Minimum Spanning Trees Shortest Path problems.](https://reader035.fdocuments.us/reader035/viewer/2022062801/56649e315503460f94b2235d/html5/thumbnails/66.jpg)
66
Depth-First Search: Example
1/
4/
2/
3/
u v w
x y z
![Page 67: 1 Introduction to Graph Data Structure Applications Graph Searching Minimum Spanning Trees Shortest Path problems.](https://reader035.fdocuments.us/reader035/viewer/2022062801/56649e315503460f94b2235d/html5/thumbnails/67.jpg)
67
Depth-First Search: Example
1/
4/
2/
3/
u v w
x y z
B
![Page 68: 1 Introduction to Graph Data Structure Applications Graph Searching Minimum Spanning Trees Shortest Path problems.](https://reader035.fdocuments.us/reader035/viewer/2022062801/56649e315503460f94b2235d/html5/thumbnails/68.jpg)
68
Depth-First Search: Example
1/
4/5
2/
3/
u v w
x y z
B
![Page 69: 1 Introduction to Graph Data Structure Applications Graph Searching Minimum Spanning Trees Shortest Path problems.](https://reader035.fdocuments.us/reader035/viewer/2022062801/56649e315503460f94b2235d/html5/thumbnails/69.jpg)
69
Depth-First Search: Example
1/
4/5
2/
3/6
u v w
x y z
B
![Page 70: 1 Introduction to Graph Data Structure Applications Graph Searching Minimum Spanning Trees Shortest Path problems.](https://reader035.fdocuments.us/reader035/viewer/2022062801/56649e315503460f94b2235d/html5/thumbnails/70.jpg)
70
Depth-First Search: Example
1/
4/5
2/7
3/6
u v w
x y z
B
![Page 71: 1 Introduction to Graph Data Structure Applications Graph Searching Minimum Spanning Trees Shortest Path problems.](https://reader035.fdocuments.us/reader035/viewer/2022062801/56649e315503460f94b2235d/html5/thumbnails/71.jpg)
71
Depth-First Search: Example
1/
4/5
2/7
3/6
u v w
x y z
BF
![Page 72: 1 Introduction to Graph Data Structure Applications Graph Searching Minimum Spanning Trees Shortest Path problems.](https://reader035.fdocuments.us/reader035/viewer/2022062801/56649e315503460f94b2235d/html5/thumbnails/72.jpg)
72
Depth-First Search: Example
1/8
4/5
2/7
3/6
u v w
x y z
BF
![Page 73: 1 Introduction to Graph Data Structure Applications Graph Searching Minimum Spanning Trees Shortest Path problems.](https://reader035.fdocuments.us/reader035/viewer/2022062801/56649e315503460f94b2235d/html5/thumbnails/73.jpg)
73
Depth-First Search: Example
1/8
4/5
2/7
3/6
9/
u v w
x y z
BFC
![Page 74: 1 Introduction to Graph Data Structure Applications Graph Searching Minimum Spanning Trees Shortest Path problems.](https://reader035.fdocuments.us/reader035/viewer/2022062801/56649e315503460f94b2235d/html5/thumbnails/74.jpg)
74
Depth-First Search: Example
1/8
4/5
2/7
3/6
9/
10/
u v w
x y z
BFC
B
![Page 75: 1 Introduction to Graph Data Structure Applications Graph Searching Minimum Spanning Trees Shortest Path problems.](https://reader035.fdocuments.us/reader035/viewer/2022062801/56649e315503460f94b2235d/html5/thumbnails/75.jpg)
75
Depth-First Search: Example
1/8
4/5
2/7
3/6
9/
10/11
u v w
x y z
BFC
B
![Page 76: 1 Introduction to Graph Data Structure Applications Graph Searching Minimum Spanning Trees Shortest Path problems.](https://reader035.fdocuments.us/reader035/viewer/2022062801/56649e315503460f94b2235d/html5/thumbnails/76.jpg)
76
Depth-First Search: Example
1/8
4/5
2/7
3/6
9/12
10/11
u v w
x y z
BFC
B
![Page 77: 1 Introduction to Graph Data Structure Applications Graph Searching Minimum Spanning Trees Shortest Path problems.](https://reader035.fdocuments.us/reader035/viewer/2022062801/56649e315503460f94b2235d/html5/thumbnails/77.jpg)
Minimum Spanning Tree (MST)
![Page 78: 1 Introduction to Graph Data Structure Applications Graph Searching Minimum Spanning Trees Shortest Path problems.](https://reader035.fdocuments.us/reader035/viewer/2022062801/56649e315503460f94b2235d/html5/thumbnails/78.jpg)
Motivation
For an electrical circuit certain pins have to be grounded. Finding the arrangement of wires (connecting those pins) that uses the least amount of wire.
Let a G(V,E) be a graph such that (u, v) E and a weight w(u,v) corresponding to wire needed to join u and v.
We are looking for an acyclic subset T E that connects all vertices and whose total weight w(T) is minimized.
TvuvuwTw
),(),()(
![Page 79: 1 Introduction to Graph Data Structure Applications Graph Searching Minimum Spanning Trees Shortest Path problems.](https://reader035.fdocuments.us/reader035/viewer/2022062801/56649e315503460f94b2235d/html5/thumbnails/79.jpg)
Motivation
Since T is acyclic and connects all the vertices, it must form a tree.
Since it spans the graph, it is called a spanning tree.
MST or actually means Minimum Weight Spanning Tree.
![Page 80: 1 Introduction to Graph Data Structure Applications Graph Searching Minimum Spanning Trees Shortest Path problems.](https://reader035.fdocuments.us/reader035/viewer/2022062801/56649e315503460f94b2235d/html5/thumbnails/80.jpg)
Definitions
The Spanning Tree◦ Subset of edges that forms a tree (connected and
acyclic), and includes every vertex Minimum Spanning Tree
◦ Spanning tree having minimum total weight on its edges
TvuvuwTw
),(),()(
![Page 81: 1 Introduction to Graph Data Structure Applications Graph Searching Minimum Spanning Trees Shortest Path problems.](https://reader035.fdocuments.us/reader035/viewer/2022062801/56649e315503460f94b2235d/html5/thumbnails/81.jpg)
Example: A Graph
![Page 82: 1 Introduction to Graph Data Structure Applications Graph Searching Minimum Spanning Trees Shortest Path problems.](https://reader035.fdocuments.us/reader035/viewer/2022062801/56649e315503460f94b2235d/html5/thumbnails/82.jpg)
Spanning tree: Cyclic
![Page 83: 1 Introduction to Graph Data Structure Applications Graph Searching Minimum Spanning Trees Shortest Path problems.](https://reader035.fdocuments.us/reader035/viewer/2022062801/56649e315503460f94b2235d/html5/thumbnails/83.jpg)
Spanning Tree: Acyclic
Is this minimum spanning tree
![Page 84: 1 Introduction to Graph Data Structure Applications Graph Searching Minimum Spanning Trees Shortest Path problems.](https://reader035.fdocuments.us/reader035/viewer/2022062801/56649e315503460f94b2235d/html5/thumbnails/84.jpg)
84
Finding Minimum Spanning Tree
Let C1 be the starting nodecompare weights of all arcs joining C1 to rest of the nodeschoose the smallest weight
arc
![Page 85: 1 Introduction to Graph Data Structure Applications Graph Searching Minimum Spanning Trees Shortest Path problems.](https://reader035.fdocuments.us/reader035/viewer/2022062801/56649e315503460f94b2235d/html5/thumbnails/85.jpg)
85
Finding Minimum Spanning Tree
Add node 2 to the spanningtree determined so far.
![Page 86: 1 Introduction to Graph Data Structure Applications Graph Searching Minimum Spanning Trees Shortest Path problems.](https://reader035.fdocuments.us/reader035/viewer/2022062801/56649e315503460f94b2235d/html5/thumbnails/86.jpg)
86
Finding Minimum Spanning Tree
Add node 2 to the spanning tree
determined so far. Compareweights of all arcs joining the spanning tree to rest of the
nodeschoose the smallest weight
arcAdd node 5 to the spanning
treedetermined so far.
![Page 87: 1 Introduction to Graph Data Structure Applications Graph Searching Minimum Spanning Trees Shortest Path problems.](https://reader035.fdocuments.us/reader035/viewer/2022062801/56649e315503460f94b2235d/html5/thumbnails/87.jpg)
87
Finding Minimum Spanning Tree
![Page 88: 1 Introduction to Graph Data Structure Applications Graph Searching Minimum Spanning Trees Shortest Path problems.](https://reader035.fdocuments.us/reader035/viewer/2022062801/56649e315503460f94b2235d/html5/thumbnails/88.jpg)
Minimum Spanning Tree
Problem: given a connected, undirected, weighted graph:
1410
3
6 4
5
2
9
15
8
![Page 89: 1 Introduction to Graph Data Structure Applications Graph Searching Minimum Spanning Trees Shortest Path problems.](https://reader035.fdocuments.us/reader035/viewer/2022062801/56649e315503460f94b2235d/html5/thumbnails/89.jpg)
Minimum Spanning Tree Problem: given a connected, undirected,
weighted graph, find a spanning tree using edges that minimize the total weight
1410
3
6 4
5
2
9
15
8
![Page 90: 1 Introduction to Graph Data Structure Applications Graph Searching Minimum Spanning Trees Shortest Path problems.](https://reader035.fdocuments.us/reader035/viewer/2022062801/56649e315503460f94b2235d/html5/thumbnails/90.jpg)
Minimum Spanning Tree
Which edges form the minimum spanning tree (MST) of the following graph?
H B C
G E D
F
A
1410
3
6 4
5
2
9
15
8
![Page 91: 1 Introduction to Graph Data Structure Applications Graph Searching Minimum Spanning Trees Shortest Path problems.](https://reader035.fdocuments.us/reader035/viewer/2022062801/56649e315503460f94b2235d/html5/thumbnails/91.jpg)
Answer:
Minimum Spanning Tree
H B C
G E D
F
A
1410
3
6 4
5
2
9
15
8
![Page 92: 1 Introduction to Graph Data Structure Applications Graph Searching Minimum Spanning Trees Shortest Path problems.](https://reader035.fdocuments.us/reader035/viewer/2022062801/56649e315503460f94b2235d/html5/thumbnails/92.jpg)
MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);
Prim’s Algorithm
![Page 93: 1 Introduction to Graph Data Structure Applications Graph Searching Minimum Spanning Trees Shortest Path problems.](https://reader035.fdocuments.us/reader035/viewer/2022062801/56649e315503460f94b2235d/html5/thumbnails/93.jpg)
MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);
Prim’s Algorithm
1410
3
6 45
2
9
15
8
Run on example graph
![Page 94: 1 Introduction to Graph Data Structure Applications Graph Searching Minimum Spanning Trees Shortest Path problems.](https://reader035.fdocuments.us/reader035/viewer/2022062801/56649e315503460f94b2235d/html5/thumbnails/94.jpg)
MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);
Prim’s Algorithm
1410
3
6 45
2
9
15
8
Run on example graph
![Page 95: 1 Introduction to Graph Data Structure Applications Graph Searching Minimum Spanning Trees Shortest Path problems.](https://reader035.fdocuments.us/reader035/viewer/2022062801/56649e315503460f94b2235d/html5/thumbnails/95.jpg)
MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);
Prim’s Algorithm
0
1410
3
6 45
2
9
15
8
Pick a start vertex r
r
![Page 96: 1 Introduction to Graph Data Structure Applications Graph Searching Minimum Spanning Trees Shortest Path problems.](https://reader035.fdocuments.us/reader035/viewer/2022062801/56649e315503460f94b2235d/html5/thumbnails/96.jpg)
MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);
Prim’s Algorithm
0
1410
3
6 45
2
9
15
8
Red vertices have been removed from Q
u
![Page 97: 1 Introduction to Graph Data Structure Applications Graph Searching Minimum Spanning Trees Shortest Path problems.](https://reader035.fdocuments.us/reader035/viewer/2022062801/56649e315503460f94b2235d/html5/thumbnails/97.jpg)
MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);
Prim’s Algorithm
0
3
1410
3
6 45
2
9
15
8
Red arrows indicate parent pointers
u
![Page 98: 1 Introduction to Graph Data Structure Applications Graph Searching Minimum Spanning Trees Shortest Path problems.](https://reader035.fdocuments.us/reader035/viewer/2022062801/56649e315503460f94b2235d/html5/thumbnails/98.jpg)
MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);
Prim’s Algorithm
14
0
3
1410
3
6 45
2
9
15
8
u
![Page 99: 1 Introduction to Graph Data Structure Applications Graph Searching Minimum Spanning Trees Shortest Path problems.](https://reader035.fdocuments.us/reader035/viewer/2022062801/56649e315503460f94b2235d/html5/thumbnails/99.jpg)
MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);
Prim’s Algorithm
14
0
3
1410
3
6 45
2
9
15
8u
![Page 100: 1 Introduction to Graph Data Structure Applications Graph Searching Minimum Spanning Trees Shortest Path problems.](https://reader035.fdocuments.us/reader035/viewer/2022062801/56649e315503460f94b2235d/html5/thumbnails/100.jpg)
MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);
Prim’s Algorithm
14
0 8
3
1410
3
6 45
2
9
15
8u
![Page 101: 1 Introduction to Graph Data Structure Applications Graph Searching Minimum Spanning Trees Shortest Path problems.](https://reader035.fdocuments.us/reader035/viewer/2022062801/56649e315503460f94b2235d/html5/thumbnails/101.jpg)
MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);
Prim’s Algorithm
10
0 8
3
1410
3
6 45
2
9
15
8u
![Page 102: 1 Introduction to Graph Data Structure Applications Graph Searching Minimum Spanning Trees Shortest Path problems.](https://reader035.fdocuments.us/reader035/viewer/2022062801/56649e315503460f94b2235d/html5/thumbnails/102.jpg)
MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);
Prim’s Algorithm
10
0 8
3
1410
3
6 45
2
9
15
8u
![Page 103: 1 Introduction to Graph Data Structure Applications Graph Searching Minimum Spanning Trees Shortest Path problems.](https://reader035.fdocuments.us/reader035/viewer/2022062801/56649e315503460f94b2235d/html5/thumbnails/103.jpg)
MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);
Prim’s Algorithm
10 2
0 8
3
1410
3
6 45
2
9
15
8u
![Page 104: 1 Introduction to Graph Data Structure Applications Graph Searching Minimum Spanning Trees Shortest Path problems.](https://reader035.fdocuments.us/reader035/viewer/2022062801/56649e315503460f94b2235d/html5/thumbnails/104.jpg)
MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);
Prim’s Algorithm
10 2
0 8 15
3
1410
3
6 45
2
9
15
8u
![Page 105: 1 Introduction to Graph Data Structure Applications Graph Searching Minimum Spanning Trees Shortest Path problems.](https://reader035.fdocuments.us/reader035/viewer/2022062801/56649e315503460f94b2235d/html5/thumbnails/105.jpg)
MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);
Prim’s Algorithm
10 2
0 8 15
3
1410
3
6 45
2
9
15
8
u
![Page 106: 1 Introduction to Graph Data Structure Applications Graph Searching Minimum Spanning Trees Shortest Path problems.](https://reader035.fdocuments.us/reader035/viewer/2022062801/56649e315503460f94b2235d/html5/thumbnails/106.jpg)
MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);
Prim’s Algorithm
10 2
0 8 15
3
4
1410
3
6 45
2
9
15
8
u
![Page 107: 1 Introduction to Graph Data Structure Applications Graph Searching Minimum Spanning Trees Shortest Path problems.](https://reader035.fdocuments.us/reader035/viewer/2022062801/56649e315503460f94b2235d/html5/thumbnails/107.jpg)
MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);
Prim’s Algorithm
5 2
0 8 15
3
4
1410
3
6 45
2
9
15
8
u
![Page 108: 1 Introduction to Graph Data Structure Applications Graph Searching Minimum Spanning Trees Shortest Path problems.](https://reader035.fdocuments.us/reader035/viewer/2022062801/56649e315503460f94b2235d/html5/thumbnails/108.jpg)
MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);
Prim’s Algorithm
5 2 9
0 8 15
3
4
1410
3
6 45
2
9
15
8
u
![Page 109: 1 Introduction to Graph Data Structure Applications Graph Searching Minimum Spanning Trees Shortest Path problems.](https://reader035.fdocuments.us/reader035/viewer/2022062801/56649e315503460f94b2235d/html5/thumbnails/109.jpg)
MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);
Prim’s Algorithm
5 2 9
0 8 15
3
4
1410
3
6 45
2
9
15
8
u
![Page 110: 1 Introduction to Graph Data Structure Applications Graph Searching Minimum Spanning Trees Shortest Path problems.](https://reader035.fdocuments.us/reader035/viewer/2022062801/56649e315503460f94b2235d/html5/thumbnails/110.jpg)
MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);
Prim’s Algorithm
5 2 9
0 8 15
3
4
1410
3
6 45
2
9
15
8
u
![Page 111: 1 Introduction to Graph Data Structure Applications Graph Searching Minimum Spanning Trees Shortest Path problems.](https://reader035.fdocuments.us/reader035/viewer/2022062801/56649e315503460f94b2235d/html5/thumbnails/111.jpg)
MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);
Prim’s Algorithm
5 2 9
0 8 15
3
4
1410
3
6 45
2
9
15
8
u
![Page 112: 1 Introduction to Graph Data Structure Applications Graph Searching Minimum Spanning Trees Shortest Path problems.](https://reader035.fdocuments.us/reader035/viewer/2022062801/56649e315503460f94b2235d/html5/thumbnails/112.jpg)
MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);
Prim’s Algorithm
5 2 9
0 8 15
3
4
1410
3
6 45
2
9
15
8
u
![Page 113: 1 Introduction to Graph Data Structure Applications Graph Searching Minimum Spanning Trees Shortest Path problems.](https://reader035.fdocuments.us/reader035/viewer/2022062801/56649e315503460f94b2235d/html5/thumbnails/113.jpg)
MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);
Review: Prim’s Algorithm
What is the hidden cost in this code?
![Page 114: 1 Introduction to Graph Data Structure Applications Graph Searching Minimum Spanning Trees Shortest Path problems.](https://reader035.fdocuments.us/reader035/viewer/2022062801/56649e315503460f94b2235d/html5/thumbnails/114.jpg)
MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u;
Review: Prim’s Algorithm
How often is ExtractMin() called?
![Page 115: 1 Introduction to Graph Data Structure Applications Graph Searching Minimum Spanning Trees Shortest Path problems.](https://reader035.fdocuments.us/reader035/viewer/2022062801/56649e315503460f94b2235d/html5/thumbnails/115.jpg)
MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);
Review: Prim’s Algorithm
What will be the running time?A: Depends on queue binary heap: O(E lg V) Fibonacci heap: O(V lg V + E)
![Page 116: 1 Introduction to Graph Data Structure Applications Graph Searching Minimum Spanning Trees Shortest Path problems.](https://reader035.fdocuments.us/reader035/viewer/2022062801/56649e315503460f94b2235d/html5/thumbnails/116.jpg)
MST-KRUSKAL(G,w)A=NULLfor each vertex v V[G]
Make-Set(v)sort the edges(E) in increasing order by weight wfor each edge (u,v) E
if FindSet(u) FindSet(v)A=A U {(u,v)}UNUION(u,v)
Kruskal’s Algo for MST
1410
3
6 45
2
9
15
8
![Page 117: 1 Introduction to Graph Data Structure Applications Graph Searching Minimum Spanning Trees Shortest Path problems.](https://reader035.fdocuments.us/reader035/viewer/2022062801/56649e315503460f94b2235d/html5/thumbnails/117.jpg)
Kruskal’s Algorithm
![Page 118: 1 Introduction to Graph Data Structure Applications Graph Searching Minimum Spanning Trees Shortest Path problems.](https://reader035.fdocuments.us/reader035/viewer/2022062801/56649e315503460f94b2235d/html5/thumbnails/118.jpg)
Graph Optimization Problems and
Greedy AlgorithmsGreedy Algorithms
// Make the best choice now!
Optimization ProblemsMinimizing Cost or Maximizing BenefitsMinimum Spanning Tree
Minimum cost for connecting all verticesSingle-Source Shortest Paths
Shortest Path between two vertices
![Page 119: 1 Introduction to Graph Data Structure Applications Graph Searching Minimum Spanning Trees Shortest Path problems.](https://reader035.fdocuments.us/reader035/viewer/2022062801/56649e315503460f94b2235d/html5/thumbnails/119.jpg)
Greedy Algorithms: Make the best choice now! Making choices in sequence such that
◦ each individual choice is best according to some limited “short-term” criterion, that is not too expensive to evaluate
◦ once a choice is made, it cannot be undone! even if it becomes evident later that it was a poor choice
Make progress by choosing an action that◦ incurs the minimum short-term cost,◦ in the hope that a lot of small short-term costs
add up to small overall cost. Possible drawback:
◦ actions with a small short-term cost may lead to a situation, where further large costs are unavoidable.
![Page 120: 1 Introduction to Graph Data Structure Applications Graph Searching Minimum Spanning Trees Shortest Path problems.](https://reader035.fdocuments.us/reader035/viewer/2022062801/56649e315503460f94b2235d/html5/thumbnails/120.jpg)
Optimization Problems Minimizing the total cost or
Maximizing the total benefits◦ Analyze all possible outcomes and find the best,
or◦ Make a series of choices whose overall effect is to
achieve the optimal. Some optimization problems can be solved
exactly by greedy algorithms◦ Minimum cost for connecting all vertices Minimum Spanning Tree Algorithm
◦ Shortest Path between two vertices Single-Source Shortest Paths Algorithm
![Page 121: 1 Introduction to Graph Data Structure Applications Graph Searching Minimum Spanning Trees Shortest Path problems.](https://reader035.fdocuments.us/reader035/viewer/2022062801/56649e315503460f94b2235d/html5/thumbnails/121.jpg)
Minimum Spanning Tree A spanning tree for a connected, undirected
graph, G=(V,E) is ◦ a subgraph of G that is ◦ an undirected tree and contains ◦ all the vertices of G.
In a weighted graph G=(V,E,W), the weight of a subgraph is ◦ the sum of the weights of the edges in the subgraph.
A minimum spanning tree for a weighted graph is ◦ a spanning tree with the minimum weight.
![Page 122: 1 Introduction to Graph Data Structure Applications Graph Searching Minimum Spanning Trees Shortest Path problems.](https://reader035.fdocuments.us/reader035/viewer/2022062801/56649e315503460f94b2235d/html5/thumbnails/122.jpg)
Prim’s Minimum Spanning Tree Algorithm Select an arbitrary starting vertex, (the root) branches out from the tree constructed so far by
◦ choosing an edge at each iteration◦ attach the edge to the tree that edge has minimum weight among all edges that can be
attached◦ add to the tree the vertex associated with the edge
During the course of the algorithm, vertices are divided into three disjoint categories:◦ Tree vertices: in the tree constructed so far,◦ Fringe vertices: not in the tree, but adjacent to some
vertex in the tree,◦ Unseen vertices: all others
![Page 123: 1 Introduction to Graph Data Structure Applications Graph Searching Minimum Spanning Trees Shortest Path problems.](https://reader035.fdocuments.us/reader035/viewer/2022062801/56649e315503460f94b2235d/html5/thumbnails/123.jpg)
The Algorithm in action, e.g.
![Page 124: 1 Introduction to Graph Data Structure Applications Graph Searching Minimum Spanning Trees Shortest Path problems.](https://reader035.fdocuments.us/reader035/viewer/2022062801/56649e315503460f94b2235d/html5/thumbnails/124.jpg)
Prim’s Minimum Spanning Trees: Outline
![Page 125: 1 Introduction to Graph Data Structure Applications Graph Searching Minimum Spanning Trees Shortest Path problems.](https://reader035.fdocuments.us/reader035/viewer/2022062801/56649e315503460f94b2235d/html5/thumbnails/125.jpg)
Properties of Minimum Spanning Trees Definition: Minimum spanning tree property
◦ Let a connected,weighted graph G=(V,E,W) be given,and let T be any spanning tree of G.
◦ Suppose that for every edge vw of G that is not in T,
◦ if uv is added to T, then it creates a cycle ◦ such that uv is a maximum-weight edge on that
cycle.◦ The the tree T is said to have the minimum
spanning tree property.
![Page 126: 1 Introduction to Graph Data Structure Applications Graph Searching Minimum Spanning Trees Shortest Path problems.](https://reader035.fdocuments.us/reader035/viewer/2022062801/56649e315503460f94b2235d/html5/thumbnails/126.jpg)
Properties of Minimum Spanning Trees … Lemma:
◦ In a connected, weighted graph G = (V, E, W), ◦ if T1 and T2 are two spanning trees that have the
MST property, ◦ then they have the same total weight.
Theorem: ◦ In a connected,weighted graph G=(V,E,W) ◦ a tree T is a minimum spanning tree if and only if ◦ T has the MST property.
![Page 127: 1 Introduction to Graph Data Structure Applications Graph Searching Minimum Spanning Trees Shortest Path problems.](https://reader035.fdocuments.us/reader035/viewer/2022062801/56649e315503460f94b2235d/html5/thumbnails/127.jpg)
Correctness of Prim’s MST Algorithm Lemma:
◦ Let G = (V, E, W) be a connected, weighted graph with n = |V|;
◦ let Tk be the tree with k vertices constructed by Prim’s algorithm, for k = 1, …, n; and
◦ let Gk be the subgraph of G induced by the vertices of Tk (i.e., uv is an edge in Gk if it is an edge in G and both u and v are in Tk).
◦ Then Tk has the MST property in Gk. Theorem:
◦Prim's algorithm outputs a minimum spanning tree.
![Page 128: 1 Introduction to Graph Data Structure Applications Graph Searching Minimum Spanning Trees Shortest Path problems.](https://reader035.fdocuments.us/reader035/viewer/2022062801/56649e315503460f94b2235d/html5/thumbnails/128.jpg)
Problem: Single-Source Shortest Paths Problem:
◦ Finding a minimum-weight path between two specified vertices
◦ It turns out that, in the worst case, it is no easier to find a minimum-weight path between a specified pair of nodes s and t than
◦ it is to find minimum-weight path between s and every vertex reachable from s. (single-source shortest paths)
![Page 129: 1 Introduction to Graph Data Structure Applications Graph Searching Minimum Spanning Trees Shortest Path problems.](https://reader035.fdocuments.us/reader035/viewer/2022062801/56649e315503460f94b2235d/html5/thumbnails/129.jpg)
Shortest-Path Definition: shortest path
◦ Let P be a nonempty path ◦ in a weighted graph G=(V,E,W) ◦ consisting of k edges xv1,v1v2,....vk-1y (possibly
v1=y).◦ The weight of P, denoted as W(P) is ◦ the sum of the weights,W(xv1),W(v1v2),...W(vk-1y).◦ If x=y, the empty path is considered to be a path
from x to y. The weight of the empty path is zero.◦ If no path between x and y has weight less than
W(P),◦ then P is called a shortest path,or minimum-
weight path.
![Page 130: 1 Introduction to Graph Data Structure Applications Graph Searching Minimum Spanning Trees Shortest Path problems.](https://reader035.fdocuments.us/reader035/viewer/2022062801/56649e315503460f94b2235d/html5/thumbnails/130.jpg)
Properties of Shortest Paths Lemma: Shortest path property
◦ In a weighted graph G, ◦ suppose that a shortest path from x to z consist of ◦ path P from x to y followed by◦ path Q from y to z.◦ Then P is a shortest path from x to y, and◦ Q is a shortest path form y to z.
![Page 131: 1 Introduction to Graph Data Structure Applications Graph Searching Minimum Spanning Trees Shortest Path problems.](https://reader035.fdocuments.us/reader035/viewer/2022062801/56649e315503460f94b2235d/html5/thumbnails/131.jpg)
Dijkstra’s Shortest-Path Algorithm
Greedy Algorithm weights are nonnegative
![Page 132: 1 Introduction to Graph Data Structure Applications Graph Searching Minimum Spanning Trees Shortest Path problems.](https://reader035.fdocuments.us/reader035/viewer/2022062801/56649e315503460f94b2235d/html5/thumbnails/132.jpg)
The Algorithm in action, e.g.
![Page 133: 1 Introduction to Graph Data Structure Applications Graph Searching Minimum Spanning Trees Shortest Path problems.](https://reader035.fdocuments.us/reader035/viewer/2022062801/56649e315503460f94b2235d/html5/thumbnails/133.jpg)
Correctness of Dijkstra’s Shortest-Path Algorithm Theorem:
◦ Let G=(V,E,W) be a weighted graph with nonnegative weights.
◦ Let V' be a subset of V and ◦ let s be a member of V'.◦ Assume that d(s,y) is the shortest distance in G from s
to y, for each yV'.◦ If edge yz is chosen to minimize d(s,y)+W(yz) over all
edges with one vertex y in V' and one vertex z in V-V',◦ then the path consisting of a shortest path from s to y
followed by the edge yz is a shortest path from s to z. Theorem:
◦ Given a directed weighted graph G with a nonnegative weights and a source vertex s, Dijkstra's algorithm computes the shortest distance from s to each vertex of G that is reachable from s.