Download presentation
Presentation is loading. Please wait.
1
Introduction to Algorithms
LECTURE 16 (Chap. 22) Elementary Graph Algorithm • 22.2 Breath-first search • 22.3 Depth-first search • 22.4 Topological sort Breadth first search animation
2
Breadth first search animation
Get ahold of a network, and use the same network to illustrate the shortest path problem for communication newtorks, the max flow problem, the minimum cost flow problem, and the multicommodity flow problem. This will be a very efficient way of introducing the four problems. (Perhaps under 10 minutes of class time.) Breadth first search animation
3
Initialize 1 2 4 5 3 6 9 7 8 1 2 4 5 3 6 9 7 8 1 1 pred(1) = 0 next := 1 order(next) = 1 LIST:= {1} Unmark all nodes in N; Mark node s LIST 1 next 1
4
Select a node i in LIST 1 2 4 5 3 6 9 7 8 1 1 1 In breadth first search, i is the first node in LIST LIST 1 next 1
5
If node i is incident to an admissible arc…
2 4 2 2 8 1 1 1 1 5 7 Next := Next + 1 order(j) := next add j to LIST Mark Node j pred(j) := i Select an admissible arc (i,j) 9 3 6 LIST 1 2 2 1 next
6
If node i is incident to an admissible arc…
2 4 2 2 8 1 1 1 1 5 5 7 3 Mark Node j pred(j) := i Select an admissible arc (i,j) Next := Next + 1 order(j) := next add j to LIST 9 3 6 LIST 1 2 5 3 2 next
7
If node i is incident to an admissible arc…
2 4 2 2 8 1 1 1 1 5 5 7 3 Select an admissible arc (i,j) Next := Next + 1 order(j) := next add j to LIST Mark Node j pred(j) := i 9 3 3 6 4 LIST 1 2 5 3 4 2 3 next
8
If node i is not incident to an admissible arc…
2 4 2 2 8 1 1 1 1 1 5 5 7 3 9 Delete node i from LIST 3 3 6 4 LIST 1 2 5 3 2 4 3 next
9
Select Node i 2 4 2 2 2 8 1 1 1 1 1 5 5 7 3 9 The first node on LIST becomes node i 3 3 6 4 LIST 1 2 5 3 2 4 3 next
10
If node i is incident to an admissible arc…
5 2 4 4 2 2 2 8 1 1 1 5 5 7 3 Select an admissible arc (i,j) Mark Node j pred(j) := i Next := Next + 1 order(j) := next add j to LIST 9 3 3 6 4 LIST 1 2 5 3 4 5 2 4 3 next
11
If node i is not incident to an admissible arc…
5 2 4 4 2 2 2 2 8 1 1 1 5 5 7 3 Delete node i from LIST 9 3 3 6 4 LIST 1 2 5 3 4 5 2 3 4 next
12
Select a node 5 2 4 4 2 2 2 8 1 1 1 5 5 5 7 3 The first node on LIST becomes node i 9 3 3 6 4 LIST 1 2 5 3 4 5 2 3 4 next
13
If node i is incident to an admissible arc…
5 2 4 4 2 2 2 8 1 1 1 5 5 5 7 3 Mark Node j pred(j) := i Select an admissible arc (i,j) Next := Next + 1 order(j) := next add j to LIST 9 3 3 6 6 4 6 LIST 1 2 5 3 4 6 3 2 4 5 next
14
If node i is not incident to an admissible arc…
5 2 4 4 2 2 2 8 1 1 1 5 5 5 5 7 3 Delete node i from LIST 9 3 3 6 6 4 6 LIST 1 2 5 3 4 6 6 3 2 4 5 next
15
Select node 3 5 2 4 4 2 2 2 8 1 1 1 5 5 5 5 7 3 node 3 is not incident to any admissible arcs delete node 3 from LIST 9 3 3 3 3 6 6 4 6 LIST 1 2 5 3 4 6 2 3 5 4 6 next
16
Select a node 5 2 4 4 4 2 2 8 1 1 1 5 5 7 3 i : = 4 9 3 3 6 6 4 6 LIST 1 2 5 3 4 6 6 2 3 4 5 next
17
If node i is incident to an admissible arc…
5 2 4 4 4 7 2 2 8 8 1 1 1 5 5 7 3 Mark Node j pred(j) := i Next := Next + 1 order(j) := next add j to LIST Select an admissible arc (i,j) 9 3 3 6 6 4 6 LIST 1 2 5 3 4 6 8 3 7 2 4 6 5 next
18
If node i is not incident to an admissible arc…
5 2 4 4 4 4 7 2 2 8 8 1 1 1 5 5 7 3 Delete node i from LIST 9 3 3 6 6 4 6 LIST 1 2 5 3 4 6 8 7 3 2 6 4 5 next
19
Select node i 5 2 4 4 7 2 2 8 8 1 1 1 5 5 7 3 i := 6 9 3 3 6 6 6 4 6 LIST 1 2 5 3 4 6 8 7 3 2 6 4 5 next
20
If node i is incident to an admissible arc…
5 2 4 4 7 2 2 8 8 1 8 1 1 5 5 7 7 3 Select an admissible arc (i,j) Mark Node j pred(j) := i Next := Next + 1 order(j) := next add j to LIST 9 3 3 6 6 6 4 6 LIST 1 2 5 3 4 6 8 7 2 8 3 5 6 4 7 next
21
If node i is incident to an admissible arc…
5 2 4 4 7 2 2 8 8 1 8 1 1 5 5 7 7 3 Select an admissible arc (i,j) Mark Node j pred(j) := i Next := Next + 1 order(j) := next add j to LIST 9 9 3 3 6 6 6 4 9 6 LIST 1 2 5 3 4 6 8 7 9 8 9 2 4 5 3 7 6 next
22
If node i is not incident to an admissible arc…
5 2 4 4 7 2 2 8 8 1 8 1 1 5 5 7 7 3 Delete node i from LIST 9 9 3 3 6 6 6 6 4 9 6 LIST 1 2 5 3 4 6 8 7 9 9 8 7 5 6 2 3 4 next
23
Select node 8 5 2 4 4 7 2 2 8 8 8 8 1 8 1 1 5 5 7 7 3 node 8 is not incident to an admissible arc; delete it from LIST 9 9 3 3 6 6 6 6 4 9 6 LIST 1 2 5 3 4 6 8 7 9 3 9 2 8 6 4 7 5 next
24
Select node 7 5 2 4 4 7 2 2 8 8 1 8 1 1 5 5 7 7 7 7 3 node 7 is not incident to an admissible arc; delete it from LIST 9 9 3 3 6 6 6 6 4 9 6 LIST 1 2 5 3 4 6 8 7 9 3 9 2 8 6 4 7 5 next
25
Select node 9 5 2 4 4 7 2 2 8 8 1 8 1 1 5 5 7 7 3 node 9 is not incident to an admissible arc; delete it from LIST 9 9 9 9 3 3 6 6 6 6 4 9 6 LIST 1 2 5 3 4 6 8 7 9 3 9 2 8 6 4 7 5 next
26
Breadth first search animation
Depth First Search Get ahold of a network, and use the same network to illustrate the shortest path problem for communication newtorks, the max flow problem, the minimum cost flow problem, and the multicommodity flow problem. This will be a very efficient way of introducing the four problems. (Perhaps under 10 minutes of class time.) Breadth first search animation
27
Initialize 1 2 4 5 3 6 9 7 8 1 2 4 5 3 6 9 7 8 1 1 pred(1) = 0 next := 1 order(next) = 1 LIST:= {1} Unmark all nodes in N; Mark node s LIST 1 next 1
28
Select a node i in LIST 1 2 4 5 3 6 9 7 8 1 1 1 In depth first search, i is the last node in LIST LIST 1 next 1
29
If node i is incident to an admissible arc…
2 4 2 2 8 1 1 1 1 5 7 Next := Next + 1 order(j) := next add j to LIST Mark Node j pred(j) := i Select an admissible arc (i,j) 9 3 6 LIST 1 2 2 1 next
30
Select the last node on LIST
2 4 2 2 2 8 1 1 1 1 1 5 7 9 3 6 Node 2 gets selected LIST 1 2 2 1 next
31
If node i is incident to an admissible arc…
2 4 4 2 2 2 3 8 1 1 1 1 1 5 7 Select an admissible arc (i,j) Next := Next + 1 order(j) := next add j to LIST Mark Node j pred(j) := i 9 3 6 LIST 1 2 4 3 2 1 next
32
Select 2 4 4 4 2 2 2 2 3 8 1 1 1 1 1 5 7 Select the last node on LIST
9 3 6 LIST 1 2 4 2 3 1 next
33
If node i is incident to an admissible arc…
2 4 4 4 2 2 2 2 3 8 8 4 1 1 1 1 1 5 7 Mark Node j pred(j) := i Select an admissible arc (i,j) Next := Next + 1 order(j) := next add j to LIST 9 3 6 LIST 1 2 4 8 3 4 2 1 next
34
Select 2 4 4 4 2 2 2 2 3 8 8 8 4 1 1 1 1 1 5 7 Select the last node on LIST 9 3 6 LIST 1 2 4 8 3 2 1 4 next
35
If node i is not incident to an admissible arc…
2 4 4 4 2 2 2 2 3 8 8 8 8 4 1 1 1 1 1 5 7 Delete node i from LIST 9 3 6 LIST 1 2 4 8 1 3 2 4 next
36
Select 2 4 4 4 4 2 2 2 2 3 8 8 8 8 4 1 1 1 1 1 5 7 Select the last node on LIST 9 3 6 LIST 1 2 4 8 1 4 2 3 next
37
If node i is incident to an admissible arc…
2 4 4 4 4 2 2 2 2 3 8 8 8 8 4 5 1 1 1 1 1 5 5 7 Mark Node j pred(j) := i Next := Next + 1 order(j) := next add j to LIST Select an admissible arc (i,j) 9 3 6 LIST 1 2 4 5 8 1 5 2 4 3 next
38
Select 2 4 4 4 4 4 2 2 2 2 3 8 8 8 8 4 5 1 1 1 1 1 5 5 5 7 Select the last node on LIST 9 3 6 LIST 1 2 4 8 5 1 2 5 3 4 next
39
If node i is incident to an admissible arc…
2 4 4 4 4 4 2 2 2 2 3 8 8 8 8 4 5 1 1 1 1 1 5 5 5 7 Select an admissible arc (i,j) Next := Next + 1 order(j) := next add j to LIST Mark Node j pred(j) := i 9 3 6 6 6 LIST 1 2 4 8 5 6 6 3 4 2 5 1 next
40
Select the last node on LIST
2 4 4 4 4 4 2 2 2 2 3 8 8 8 8 4 5 1 1 1 1 1 5 5 5 5 7 Select node 6 9 3 6 6 6 6 LIST 1 2 4 8 5 6 3 4 6 5 1 2 next
41
If node i is incident to an admissible arc…
2 4 4 4 4 4 2 2 2 2 3 8 8 8 8 4 5 1 1 1 1 1 5 5 5 5 7 Mark Node j pred(j) := i Select an admissible arc (i,j) Next := Next + 1 order(j) := next add j to LIST 9 9 3 6 6 6 7 6 LIST 1 2 4 5 8 6 9 1 7 2 4 5 3 6 next
42
Select the last node on LIST
2 4 4 4 4 4 2 2 2 2 3 8 8 8 8 4 5 1 1 1 1 1 5 5 5 5 7 Select node 9 9 9 9 3 6 6 6 6 7 6 LIST 1 2 4 5 8 6 9 7 1 2 4 6 5 3 next
43
If node i is incident to an admissible arc…
2 4 4 4 4 4 2 2 2 2 3 8 8 8 8 4 5 1 8 1 1 1 1 5 5 5 5 7 7 Select an admissible arc (i,j) Next := Next + 1 order(j) := next add j to LIST Mark Node j pred(j) := i 9 9 9 3 6 6 6 6 7 6 LIST 1 2 4 5 8 6 9 7 8 4 3 1 6 2 5 7 next
44
Select the last node on LIST
2 4 4 4 4 4 2 2 2 2 3 8 8 8 8 4 5 1 8 1 1 1 1 5 5 5 5 7 7 7 Select node 7 9 9 9 9 3 6 6 6 6 7 6 LIST 1 2 4 5 8 6 9 7 4 8 1 6 2 3 5 7 next
45
If node i is not incident to an admissible arc…
2 4 4 4 4 4 2 2 2 2 3 8 8 8 8 4 5 1 8 1 1 1 1 5 5 5 5 7 7 7 7 Delete node 7 from LIST 9 9 9 9 3 6 6 6 6 7 6 LIST 1 2 4 8 5 6 9 7 1 2 8 4 6 3 7 5 next
46
Select node 9 2 4 4 4 4 4 2 2 2 2 3 8 8 8 8 4 5 1 8 1 1 1 1 5 5 5 5 7 7 7 7 Delete node 9 from LIST But node 9 is not incident to an admissible arc. 9 9 9 9 9 9 3 6 6 6 6 7 6 LIST 1 2 4 8 5 6 9 7 8 4 2 1 7 3 5 6 next
47
Select node 6 2 4 4 4 4 4 2 2 2 2 3 8 8 8 8 4 5 1 8 1 1 1 1 5 5 5 5 7 7 7 7 But node 6 is not incident to an admissible arc. Delete node 6 from LIST 9 9 9 9 9 9 3 6 6 6 6 6 6 7 6 LIST 1 2 4 5 8 6 9 7 4 3 2 6 7 8 1 5 next
48
Select node 5 2 4 4 4 4 4 2 2 2 2 3 8 8 8 8 4 5 1 8 1 1 1 1 5 5 5 5 5 5 7 7 7 7 But node 5 is not incident to an admissible arc. Delete node 5 from LIST 9 9 9 9 9 9 3 6 6 6 6 6 6 7 6 LIST 1 2 4 8 5 6 9 7 3 2 1 4 5 6 8 7 next
49
Select node 4 2 4 4 4 4 4 4 4 2 2 2 2 3 8 8 8 8 4 5 1 8 1 1 1 1 5 5 5 5 5 5 7 7 7 7 Delete node 4 from LIST But node 4 is not incident to an admissible arc. 9 9 9 9 9 9 3 6 6 6 6 6 6 7 6 LIST 1 2 4 5 8 6 9 7 7 1 2 5 8 4 6 3 next
50
Select node 2 2 4 4 4 4 4 4 4 2 2 2 2 2 2 3 8 8 8 8 4 5 1 8 1 1 1 1 5 5 5 5 5 5 7 7 7 7 But node 2 is not incident to an admissible arc. Delete node 2 from LIST 9 9 9 9 9 9 3 6 6 6 6 6 6 7 6 LIST 1 2 4 5 8 6 9 7 6 3 1 4 5 7 2 8 next
51
Select node 1 2 4 4 4 4 4 4 4 2 2 2 2 2 2 3 8 8 8 8 4 5 1 8 1 1 1 1 1 5 5 5 5 5 5 7 7 7 7 Next := Next + 1 order(j) := next add j to LIST Mark Node j pred(j) := i Select an admissible arc (i,j) 9 9 9 9 9 9 3 3 6 6 6 6 6 6 7 9 6 LIST 1 3 2 4 8 5 6 9 7 2 6 9 7 3 8 5 1 4 next
52
Select node 3 2 4 4 4 4 4 4 4 2 2 2 2 2 2 3 8 8 8 8 4 5 1 8 1 1 1 1 1 1 5 5 5 5 5 5 7 7 7 7 Delete node 3 from LIST But node 3 is not incident to an admissible arc. 9 9 9 9 9 9 3 3 3 3 6 6 6 6 6 6 7 9 6 LIST 1 3 2 4 5 8 6 9 7 9 8 5 3 4 2 1 7 6 next
53
Select node 1 2 4 4 4 4 4 4 4 2 2 2 2 2 2 3 8 8 8 8 4 5 1 8 1 1 1 1 1 1 1 1 5 5 5 5 5 5 7 7 7 7 Delete node 1 from LIST But node 1 is not incident to an admissible arc. 9 9 9 9 9 9 3 3 3 3 6 6 6 6 6 6 7 9 6 LIST 1 2 3 4 8 5 6 9 7 9 5 2 1 3 4 7 6 8 next
54
LIST is empty 2 4 4 4 4 4 4 4 2 2 2 2 2 2 3 8 8 8 8 4 5 1 8 1 1 1 1 1 1 1 1 5 5 5 5 5 5 7 7 7 7 The algorithm ends! 9 9 9 9 9 9 3 3 3 3 6 6 6 6 6 6 7 9 6 LIST 1 2 3 4 5 8 6 9 7 9 4 5 3 6 2 1 7 8 next
55
The depth first search tree
1 3 2 9 8 7 5 4 6 Note that each induced subtree has consecutively labeled nodes
56
Breadth first search animation
Topological Ordering Get ahold of a network, and use the same network to illustrate the shortest path problem for communication newtorks, the max flow problem, the minimum cost flow problem, and the multicommodity flow problem. This will be a very efficient way of introducing the four problems. (Perhaps under 10 minutes of class time.) Breadth first search animation
57
Preliminary to Topological Sorting
LEMMA. If each node has at least one arc going out, then the first inadmissible arc of a depth first search determines a directed cycle. COROLLARY 1. If G has no directed cycle, then there is a node in G with no arcs going. And there is at least one node in G with no arcs coming in. COROLLARY 2. If G has no directed cycle, then one can relabel the nodes so that for each arc (i,j), i < j. 1 4 6 7 3
58
Initialization 6 1 Determine the indegree of each node
LIST is the set of nodes with indegree of 0. “Next” will be the label of nodes in the topological order. 5 2 8 3 7 4 next 1 2 3 4 5 6 7 8 Node Indegree LIST 7
59
Select a node from LIST 6 1 next := next +1 order(i) := next;
update indegrees update LIST Select a node from LIST and delete it. 5 2 8 3 7 7 4 1 1 next 1 2 3 4 5 6 7 8 Node LIST Indegree 2 2 3 2 1 1 1 2 7 5
60
Select a node from LIST 6 1 next := next +1 order(i) := next;
update indegrees update LIST Select a node from LIST and delete it. 2 5 5 2 8 3 7 7 4 1 1 2 next 1 2 3 4 5 6 7 8 Node LIST Indegree 2 2 1 3 2 1 1 1 2 4 5 7 6
61
Select a node from LIST 3 6 6 1 next := next +1 order(i) := next;
update indegrees update LIST Select a node from LIST and delete it. 2 5 5 2 8 3 7 7 4 1 2 1 3 next 1 2 3 4 5 6 7 8 Node LIST Indegree 1 2 2 1 3 1 2 1 1 2 4 5 7 2 6
62
Select a node from LIST 3 6 6 1 next := next +1 order(i) := next;
update indegrees update LIST Select a node from LIST and delete it. 2 5 5 2 2 8 3 4 7 7 4 1 4 3 2 1 next 1 2 3 4 5 6 7 8 Node LIST Indegree 1 2 2 1 3 2 1 1 1 2 4 7 5 6 2 1
63
Select a node from LIST 5 3 6 6 1 1 next := next +1 order(i) := next;
update indegrees update LIST Select a node from LIST and delete it. 2 5 5 2 2 8 3 4 7 7 4 1 3 5 2 4 1 next 1 2 3 4 5 6 7 8 Node LIST Indegree 1 2 1 2 2 3 2 1 1 1 2 1 4 7 5 1 2 6
64
Select a node from LIST 5 3 6 6 1 1 next := next +1 order(i) := next;
update indegrees update LIST Select a node from LIST and delete it. 2 5 5 2 2 8 3 4 7 7 4 4 1 6 6 3 5 4 2 1 next 1 2 3 4 5 6 7 8 Node LIST Indegree 1 2 2 1 3 2 1 1 2 1 1 2 1 2 1 4 6 7 5 8
65
Select a node from LIST 5 3 6 6 1 1 next := next +1 order(i) := next;
update indegrees update LIST Select a node from LIST and delete it. 7 2 5 5 2 2 8 8 3 4 7 7 4 4 1 6 2 7 4 5 3 6 1 next 1 2 3 4 5 6 7 8 Node LIST Indegree 1 2 1 2 2 3 1 1 2 1 1 1 2 8 3
66
Select a node from LIST 5 3 6 6 1 1 next := next +1 order(i) := next;
update indegrees update LIST Select a node from LIST and delete it. 7 8 2 5 5 2 2 8 8 3 3 4 7 7 4 4 1 6 2 5 8 6 7 3 1 4 next List is empty. The algorithm terminates with a topological order of the nodes 1 2 3 4 5 6 7 8 Node LIST Indegree 1 2 2 1 2 1 3 1 2 1 1 2 1 3
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.