lecture_graph_greedy
lecture_graph_greedy
• Breadth First Traversal (or Search) for a graph is similar to Breadth First
Traversal of a tree The only catch here is, unlike trees, graphs may
contain cycles, so we may come to the same node again. To avoid
processing a node more than once, we use a Boolean visited array. For
simplicity, it is assumed that all vertices are reachable from the starting
vertex.
• Breadth-first search is a way to find all the vertices reachable from a
given source vertex, s.
• As the name BFS suggests, you are required to traverse the graph
breadth wise : First move horizontally and visit all the nodes of the
current level then move to the next level
c.
e.
g. The algorithm discovers all vertices 3 edges from s i.e., discovered all
vertices (u and y) at level 3.
h.
i. The algorithm terminates when every vertex has been fully explored.
Depth First Traversal (or Search) for a graph is similar to Depth First
Traversal of a tree. The only catch here is, unlike trees, graphs may contain
cycles, a node may be visited twice. To avoid processing a node more than
once, use a Boolean visited array.
Like BFS, to keep track of progress depth-first-search colors each vertex.
Each vertex of the graph is in one of three states:
• Undiscovered
• Discovered but not finished (not done exploring from it); and
• Finished (have found everything reachable from it) i.e. fully explored.
The DFS forms a depth-first forest comprised of more than one depth-first
trees. Each tree is made of edges (u, v) such that u is gray and v is white when
edge (u, v) is explored. The following pseudocode for DFS uses a global
timestamp time.
DFS (V, E)
1. for each vertex u in V[G]
2. do color[u] ← WHITE
3. π[u] ← NIL
4. time ← 0
5. for each vertex u in V[G]
6. do if color[u] ← WHITE
7. then DFS-Visit(u)
Example
In the following figure, the solid edge represents discovery or tree edge and the
dashed edge shows the back edge. Furthermore, each vertex has two time stamps:
the first time-stamp records when vertex is first discovered and second time-stamp
records when the search finishes examining adjacency list of vertex.
Example Cond…
B 210 B
A A
450
60 190
unweighted
graph
C C weighted
200 graph
130
D D
E E
• How can we find the shortest route between two points on a map?
• Model the problem as a graph problem:
• Road map is a weighted graph:
vertices = cities
edges = road segments between cities
edge weights = road distances
• Goal: find a shortest path between two vertices (cities)
• Shortest-path weight
p from u to v:
∞ otherwise
• All-pairs shortest-paths
• Find a shortest path from u to v for every pair of vertices u and v
Given: vj
pij pjk
• A weighted, directed graph G = (V, E) v1
p1i
• A weight function w: E R, pij’ vk
Shortest-Path Representation
Initialization
Alg.: INITIALIZE-SINGLE-SOURCE(V, s)
1. for each v V
2. do d[v] ←
3. [v] ← NIL
4. d[s] ← 0
Relaxation
• Relaxing an edge (u, v) = testing whether we can improve the shortest path to v
found so far by going through u
If d[v] > d[u] + w(u, v)
we can improve the shortest path to v
update d[v] and [v]
s s
u v u v
2 2
5 9 5 6 After relaxation:
d[v] d[u] + w(u,
RELAX(u, v, w) v)
RELAX(u, v, w)
u v u v
2 2
5 7 5 6
RELAX(u, v, w)
• The algorithms differ in the order and how many times they relax each edge
Dijkstra’s Algorithm
• Single-source shortest path problem:
• No negative-weight edges: w(u, v) > 0 (u, v) E
Dijkstra (G, w, s) t x
1
1. INITIALIZE-SINGLE-SOURCE(V, s)
10 9
2. S← 2 4
s 3 6
0
3. Q ← V[G]
5 7
4. while Q
2
y z
5. do u ← EXTRACT-MIN(Q) t x
1
6. S ← S {u}
10
10 9
7. for each vertex v Adj[u] 2 3 4 6
s 0
8. do RELAX(u, v, w) 5 7
5
2
y z
Example
t x t 1 x
1
8
10 14
8 13
14
10 9 10 9
2 3 4 6 2 3 4 6
s 0 s 0
5 7 5 7
5 7
5 7
2 2
y z y z
t x t 1 x
1
8 13
9 8 9
10 9 10 9
2 4 2 3 4 6
s 0 3 6 s 0
7 5 7
5
5 7 5 7
2 2
y z y z
24
2 3
9
s
18
14
2 6
6
30 4 19
11
15 5
5
6
20 16
t
7 44
S={ }
PQ = { s, 2, 3, 4, 5, 6, 7, t }
24
2 3
0 9
s
18
14 2 6
6
30 4 19
11
15 5
5
6
20 16
t
7 44
distance label
32 Data Structures (ODD SEM 2020)
Source for Example and algorithms :Introduction to Algorithms, 3rd Edition
(The MIT Press) 3rd Edition by Thomas H. Cormen, Charles E. Leiserson, R
onald L. Rivest , Clifford Stein
s
18
14 2 6
6
30 4 19
11
15 5
5
6
20 16
t
7 44
distance label
33 Data Structures (ODD SEM 2020)
Source for Example and algorithms :Introduction to Algorithms, 3rd Edition
(The MIT Press) 3rd Edition by Thomas H. Cormen, Charles E. Leiserson, R
onald L. Rivest , Clifford Stein
s
18
14 X
14 6
2
6
30 4 19
11
15 5
5
6
20 16
t
7 44
distance label 15
X
34 Data Structures (ODD SEM 2020)
Source for Example and algorithms :Introduction to Algorithms, 3rd Edition
(The MIT Press) 3rd Edition by Thomas H. Cormen, Charles E. Leiserson, R
onald L. Rivest , Clifford Stein
s
18
14 X
14 6
2
6
30 4 19
11
15 5
5
6
20 16
t
7 44
distance label 15
X
35 Data Structures (ODD SEM 2020)
Source for Example and algorithms :Introduction to Algorithms, 3rd Edition
(The MIT Press) 3rd Edition by Thomas H. Cormen, Charles E. Leiserson, R
onald L. Rivest , Clifford Stein
s
18
14 X
14 6
2
6
30 4 19
11
15 5
5
6
20 16
t
7 44
15
X
36 Data Structures (ODD SEM 2020)
Source for Example and algorithms :Introduction to Algorithms, 3rd Edition
(The MIT Press) 3rd Edition by Thomas H. Cormen, Charles E. Leiserson, R
onald L. Rivest , Clifford Stein
X 33
9
X
24
2 3
0 9
s
18
14 X
14 6
2
6
30 4 19
11
15 5
5
6
20 16
t
7 44
15
X
37 Data Structures (ODD SEM 2020)
Source for Example and algorithms :Introduction to Algorithms, 3rd Edition
(The MIT Press) 3rd Edition by Thomas H. Cormen, Charles E. Leiserson, R
onald L. Rivest , Clifford Stein
X 33
9
X
24
2 3
0 9
delmin
s
18
14 X
14 6
2
6
30 4 19
11
15 5
5
6
20 16
t
7 44
15
X
38 Data Structures (ODD SEM 2020)
Source for Example and algorithms :Introduction to Algorithms, 3rd Edition
(The MIT Press) 3rd Edition by Thomas H. Cormen, Charles E. Leiserson, R
onald L. Rivest , Clifford Stein
s
18
14 X
14 6
2
6
44
30 X
4 19
11
15 5
5
6
20 16
t
7 44
15
X
39 Data Structures (ODD SEM 2020)
Source for Example and algorithms :Introduction to Algorithms, 3rd Edition
(The MIT Press) 3rd Edition by Thomas H. Cormen, Charles E. Leiserson, R
onald L. Rivest , Clifford Stein
s
18
14 X
14 6
2
6
44
30 X
4 19
11
15 5
5
6
20 16
t
7 44
15
X delmin
40 Data Structures (ODD SEM 2020)
Source for Example and algorithms :Introduction to Algorithms, 3rd Edition
(The MIT Press) 3rd Edition by Thomas H. Cormen, Charles E. Leiserson, R
onald L. Rivest , Clifford Stein
s
18
14 X
14 6
2
6
X 35
44
30 X
4 19
11
15 5
5
6
20 16
t
7 44
15
X
59 X
41 Data Structures (ODD SEM 2020)
Source for Example and algorithms :Introduction to Algorithms, 3rd Edition
(The MIT Press) 3rd Edition by Thomas H. Cormen, Charles E. Leiserson, R
onald L. Rivest , Clifford Stein
s
18
14 X
14 6
2
6
X 35
44
30 X
4 19
11
15 5
5
6
20 16
t
7 44
15
X
59 X
42 Data Structures (ODD SEM 2020)
Source for Example and algorithms :Introduction to Algorithms, 3rd Edition (The MIT
Press) 3rd Edition by Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest , Clifford
Stein
s
18
14 X
14 6
2
6
X 35
44 X 34
30 X
4 19
11
15 5
5
6
20 16
t
7 44
15
X 51 59
X X
43 Data Structures (ODD SEM 2020)
Source for Example and algorithms :Introduction to Algorithms, 3rd Edition (The MIT
Press) 3rd Edition by Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest , Clifford
Stein
s
18
14 X
14 6
2
6
X 35
44 X 34
30 X
4 19
11
15 5
5
6
20 16
delmin
t
7 44
15
X 51 59
X X
44 Data Structures (ODD SEM 2020)
Source for Example and algorithms :Introduction to Algorithms, 3rd Edition (The MIT
Press) 3rd Edition by Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest , Clifford
Stein
s
18
14 X
14 6
2
6
45 X
X 35
44 X 34
30 X
4 19
11
15 5
5
6
20 16
t
7 44
15
X 50 51
X 59
X X
45 Data Structures (ODD SEM 2020)
Source for Example and algorithms :Introduction to Algorithms, 3rd Edition (The MIT
Press) 3rd Edition by Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest , Clifford
Stein
s
18
14 X
14 6
2
6
45 X
X 35
44 X 34
30 X
4 19
11
15 5 delmin
5
6
20 16
t
7 44
15
X 50 51
X 59
X X
46 Data Structures (ODD SEM 2020)
Source for Example and algorithms :Introduction to Algorithms, 3rd Edition
(The MIT Press) 3rd Edition by Thomas H. Cormen, Charles E. Leiserson, R
onald L. Rivest , Clifford Stein
s
18
14 X
14 6
2
6
45 X
X 35
44 X 34
30 X
4 19
11
15 5
5
6
20 16
t
7 44
15
X 50 51
X 59
X X
47 Data Structures (ODD SEM 2020)
Source for Example and algorithms :Introduction to Algorithms, 3rd Edition
(The MIT Press) 3rd Edition by Thomas H. Cormen, Charles E. Leiserson, R
onald L. Rivest , Clifford Stein
s
18
14 X
14 6
2
6
45 X
X 35
44 X 34
30 X
4 19
11
15 5
5
6
20 16
t
7 44
15
X
delmin 50 51
X 59
X X
48 Data Structures (ODD SEM 2020)
Source for Example and algorithms :Introduction to Algorithms, 3rd Edition (The MIT Press) 3rd
Edition by Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest , Clifford Stein
s
18
14 X
14 6
2
6
45 X
X 35
44 X 34
30 X
4 19
11
15 5
5
6
20 16
t
7 44
15
X 50 51
X 59
X X
49 Data Structures (ODD SEM 2020)
Source for Example and algorithms :Introduction to Algorithms, 3rd Edition (The MIT Press) 3rd
Edition by Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest , Clifford Stein
s
18
14 X
14 6
2
6
45 X
X 35
44 X 34
30 X
4 19
11
15 5
5
6
20 16
t
7 44
15
X 50 51
X 59
X X
50 Data Structures (ODD SEM 2020)
Source for Example and algorithms :Introduction to Algorithms, 3rd Edition (The MIT
Press) 3rd Edition by Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest , Clifford
Stein
51
Dijkstra (G, w, s)
1. INITIALIZE-SINGLE-SOURCE(V, s)
(V)
2. S←
3. Q ← V[G] O(V) build min-heap
4. while Q Executed O(V) times
5. do u ← EXTRACT-MIN(Q) O(lgV)
6. S ← S {u}
7. for each vertex v Adj[u]
8. do RELAX(u, v, w)
Running time: O(VlgV + ElgV) = O(ElgV) O(E) times; O(lgV)
- Routing Systems
Spanning Tree
• Spanning Trees: A sub graph T of a undirected graph G= {V,E } is a
spanning tree if it is a tree and contains every vertex of G.
Remark: The minimum spanning tree may not be unique. However, if the weights
of all the edges are pairwise distinct, it is indeed unique.
MST Problem
Generic MST
• Let A be a set of edges such that A ⊆ T, where T is a MST. An edge (u,v) is a safe edge
for A, if A ∪ {(u,v)} is also a subset of some MST. If at each step, we can find a safe
edge , we can ’grow’ a MST. This leads to the following generic approach:
• Generic-MST(G, w) :
Let A=EMPTY;
while A does not form a spanning tree find an edge (u, v) that is safe for A add (u, v).
return A
* Safe edge :
Let G = (V,E) be a connected, undirected graph with a real-valued weight function w
defined on E . Let A be a subset of E that is included in some minimum spanning tree for
G, let (S, S-V) be any cut of G that respects A , and let (u,v) be a light edge crossing
the cut (S,S-V). Then, edge (u,v) is safe for A.
Prim’s Algorithm
Grow a Tree
• Start by picking any vertex r to be the root of the tree.
• While the tree does not contain all vertices in the graph find shortest edge
leaving the tree and add it to the tree .
Kruskal’s Algorithm
References