chapter 13: graphs
DESCRIPTION
Chapter 13: Graphs. Graph ADT. Topological Sort. Shortest Path. Maximum Flow. Minimum Spanning Tree. Depth-First Search. P And NP Problems. CS 240. 237. 4. 3. 6. 6. 5. Undirected, unweighted , unconnected, loopless graph (length of longest simple path: 2). - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Chapter 13: Graphs](https://reader035.vdocuments.us/reader035/viewer/2022062323/568164b8550346895dd6c4b6/html5/thumbnails/1.jpg)
Chapter 13: Graphs
GRAPH ADT
TOPOLOGICAL SORT
CS 240 1
SHORTEST PATH
MAXIMUM FLOW
MINIMUM SPANNING TREE
DEPTH-FIRST SEARCH
P AND NP PROBLEMS
![Page 2: Chapter 13: Graphs](https://reader035.vdocuments.us/reader035/viewer/2022062323/568164b8550346895dd6c4b6/html5/thumbnails/2.jpg)
CS 240 2
Graph DefinitionA graph G = (V, E) consists of a set of vertices, V, and a set of edges, E, each of which is a pair of vertices. If the edges are ordered pairs of vertices, then the graph is directed.
Undirected, unweighted, unconnected, loopless graph
(length of longest simple
path: 2)
Directed, unweighted,
acyclic, weakly
connected, loopless graph
(length of longest simple
path: 6)
Directed, unweighted,
cyclic, strongly
connected, loopless graph
(length of longest simple
cycle: 7)
Undirected, unweighted, connected graph, with
loops(length of
longest simple path: 4)
Directed,weighted,
cyclic, weakly connected,
loopless graph(weight of
longest simple cycle: 27)
3 4
2
4
23
5
6
3
6 5
Graphs are relatively unstructured, but they provide a platform for solving some of the most sophisticated problems in computer science.
![Page 3: Chapter 13: Graphs](https://reader035.vdocuments.us/reader035/viewer/2022062323/568164b8550346895dd6c4b6/html5/thumbnails/3.jpg)
CS 240 3
Graph RepresentationsAdjacency Matrix:
A B
D
H
E
G
F
C
ABCDEFGH
A B C D E F G H11000010
10110000
01000000
01000000
00001010
00000010
10001101
00000010
A B
D
H
E
G
F
C
ABCDEFGH
A B C D E F G H00000100
10100000
00001000
11000000
00010001
00010000
00010100
00000010
A B
D
H
E
G
F
C3 4
2
4
23
5
6
3
6 5
ABCDEFGH
A B C D E F G H2
35
4
6
62
345
3
The Problem: Most graphs are sparse (i.e., most vertex pairs are not edges), so the memory requirement is excessive (i.e., V2).
![Page 4: Chapter 13: Graphs](https://reader035.vdocuments.us/reader035/viewer/2022062323/568164b8550346895dd6c4b6/html5/thumbnails/4.jpg)
CS 240 4
Graph RepresentationsAdjacency List:
ABCDEFGH
AABBEGAG
BC
G
E
GD
F H
A B
D
H
E
G
F
C
ABCDEFGH
B
B 3H
G
6
2
4
C
A
3
53
5
4
2
D
E
G
G
6E
A B
D
H
E
G
F
C3
4
2
4
2
3
5
6
3
65
ABCDEFGH
B
EB
GCA
E
D
F
G
D
H
A B
D
H
E
G
F
C
![Page 5: Chapter 13: Graphs](https://reader035.vdocuments.us/reader035/viewer/2022062323/568164b8550346895dd6c4b6/html5/thumbnails/5.jpg)
CS 240 5
Topological SortA topological sort of an acyclic directed graph orders the vertices so that if there is a path from vertex u to vertex v, then vertex v appears after vertex u in the ordering.
One topological sort of the course prerequisite graph at right:
MATH 120, CS 111, MATH 125, CS 140, MATH 150, CS 150, MATH 224, CS 240, CS 234, MATH 152, CS 312, ECE 282, STAT 380, CS 321, MATH 250, MATH 321, CS 314, MATH 423, CS 325, CS 340, CS 425, ECE 381, CS 434, ECE 482, CS 330, CS 382, CS 423, CS 438, CS 454, CS 447, CS 499, CS 482, CS 456, ECE 483
MATH 125 CS 140
CS 150MATH 152
CS 240 CS 234
MATH 224
CS 340
STAT 380
CS 438
CS 454
CS 456
CS 325
CS 499
CS 434
CS 312
CS 482
CS 330
CS 423CS 447
MATH 150
CS 314
CS 425
CS 321
ECE 483
ECE 282ECE 381
ECE 482
CS 111
MATH 120
MATH 250
MATH 321
MATH 423
CS 382
![Page 6: Chapter 13: Graphs](https://reader035.vdocuments.us/reader035/viewer/2022062323/568164b8550346895dd6c4b6/html5/thumbnails/6.jpg)
CS 240 6
Topological Sort Algorithm Place all indegree-zero vertices
in a list. While the list is non-empty:
– Output an element v in the indegree-zero list.
– For each vertex w with (v,w) in the edge set E:• Decrement the indegree of w
by one.• Place w in the indegree-zero
list if its new indegree is 0.
G
A
H I
E F D
CB
013114130
03104030
021
3020
020
201
10
201
0
101
0
1
0
1
0
Indegree of A:Indegree of B:Indegree of C:Indegree of D:Indegree of E:Indegree of F:
Indegree of G:Indegree of H:Indegree of I:
Output: A E I B D G C H
0
F
![Page 7: Chapter 13: Graphs](https://reader035.vdocuments.us/reader035/viewer/2022062323/568164b8550346895dd6c4b6/html5/thumbnails/7.jpg)
CS 240 7
Shortest Path ProblemHow do you find the shortest path from a specified vertex to every other vertex in the directed graph?
Game Pathfinding
Robotic Maneuvering Web-Based Mapping
NetworkRouting
![Page 8: Chapter 13: Graphs](https://reader035.vdocuments.us/reader035/viewer/2022062323/568164b8550346895dd6c4b6/html5/thumbnails/8.jpg)
CS 240 8
G
A
H I
E F
CB
D(0,-)
(0,-)
(1,A)
(1,A)
(1,A)
(2,B)
(2,E)
(2,E)
(3,F)
G
A
H I
E F
CB
D
(0,-)
(1,A)
(1,A)
(1,A)
G
A
H I
E F
CB
D (0,-)
(1,A)
(1,A)
(1,A)
(2,B)
(2,E)
(2,E)
G
A
H I
E F
CB
D
(0,-)
(1,A)
(1,A)
(1,A)
(2,B)
(2,E)
(2,E)
(3,F)
(4,D)
G
A
H I
E F
CB
D
Case 1: Unweighted GraphsUse a breadth-first search, i.e., starting at the specified vertex, mark each adjacent vertex with its distance and its predecessor, until all vertices are marked. Algorithm’s time complexity: O(E+V).
Shortest Path Algorithms
![Page 9: Chapter 13: Graphs](https://reader035.vdocuments.us/reader035/viewer/2022062323/568164b8550346895dd6c4b6/html5/thumbnails/9.jpg)
CS 240 9
Case 2: Weighted Graphs With No Negative WeightsUse Dijkstra’s Algorithm, i.e., starting at the specified vertex, finalize the unfinalized vertex whose current cost is minimal, and update each vertex adjacent to the finalized vertex with its (possibly revised) cost and predecessor, until all vertices are finalized. Algorithm’s time complexity: O(E+V2) = O(V2).
G
A
H I
E F
CB
D13
7
6
8
19
21
168
2
25
3
4 3
(0,-)
(,-)
(,-)
(,-)
(,-)
(,-) (,-
)
(,-)
(,-)
(0,-)
(7,A)
(26,B)
(6,A)
(19,E)
(8,A) (22,E
)
(,-)
(,-)G
A
H I
E F
CB
D13
7
6
8
19
21
168
2
25
3
4 3
(0,-)
(7,A)
(27,E)
(6,A)
(19,E)
(8,A) (22,E
)
(,-)
(,-)G
A
H I
E F
CB
D13
7
6
8
19
21
168
2
25
3
4 3
(0,-)
(7,A)
(26,B)
(6,A)
(18,H)
(8,A) (16,G)
(,-)
(,-)G
A
H I
E F
CB
D13
7
6
8
19
21
168
2
25
3
4 3
(0,-)
(7,A)
(,-)
(6,A)
(,-)
(8,A) (,-)
(,-)
(,-)G
A
H I
E F
CB
D13
7
6
8
19
21
168
2
25
3
4 3
(0,-)
(7,A)
(26,B)
(6,A)
(19,E)
(8,A) (16,G)
(,-)
(,-)G
A
H I
E F
CB
D13
7
6
8
19
21
168
2
25
3
4 3
![Page 10: Chapter 13: Graphs](https://reader035.vdocuments.us/reader035/viewer/2022062323/568164b8550346895dd6c4b6/html5/thumbnails/10.jpg)
CS 240 10
Note that this algorithm would not work for graphs with negative weights, since a vertex cannot be finalized when there might be some negative weight in the graph which would reduce a particular path’s cost.
(0,-)
(7,A)
(25,D)
(6,A)
(18,H)
(8,A) (16,G)
(20,F)
(23,D)
G
A
H I
E F
CB
D13
7
6
8
19
21
168
2
25
3
4 3
(0,-)
(7,A)
(26,B)
(6,A)
(18,H)
(8,A) (16,G)
(20,F)
(,-)G
A
H I
E F
CB
D13
7
6
8
19
21
168
2
25
3
4 3
(0,-)
(7,A)
(25,D)
(6,A)
(18,H)
(8,A) (16,G)
(20,F)
(23,D)
G
A
H I
E F
CB
D13
7
6
8
19
21
168
2
25
3
4 3
(0,-)
(7,A)
(25,D)
(6,A)
(18,H)
(8,A) (16,G)
(20,F)
(23,D)
G
A
H I
E F
CB
D13
7
6
8
19
21
168
2
25
3
4 3
![Page 11: Chapter 13: Graphs](https://reader035.vdocuments.us/reader035/viewer/2022062323/568164b8550346895dd6c4b6/html5/thumbnails/11.jpg)
CS 240 11
Case 3: Weighted Graphs With Negative WeightsUse a variation of Dijkstra’s Algorithm without using the concept of vertex finalization, i.e., starting at the specified vertex, update each vertex adjacent to the current vertex with its (possibly revised) cost and predecessor, placing each revised vertex in a queue. Continue until the queue is empty. Algorithm’s time complexity: O(EV).
G
A
H I
E F
CB
D
1325
26
15
-27
-9
6
11
6-9
-3
-4 8
(0,-)
(,-)
(,-)
(,-)
(,-)
(,-) (,-
)
(,-)
(,-)
(0,-)
(13,A)
(28,B)
(25,A)
(20,B)
(26,A) (,-
)
(,-)
(,-)
G
A
H I
E F
CB
D
1325
26
15
-27
-9
6
11
6-9
-3
-4 8
(0,-)
(13,A)
(,-)
(25,A)
(23,E)
(26,A) (,-
)
(,-)
(,-)
G
A
H I
E F
CB
D
1325
26
15
-27
-9
6
11
6-9
-3
-4 8
(0,-)
(13,A)
(28,B)
(25,A)
(20,B)
(26,A) (31,F)
(26,F)
(,-)
G
A
H I
E F
CB
D
1325
26
15
-27
-9
6
11
6-9
-3
-4 8
(0,-)
(13,A)
(,-)
(25,A)
(,-)
(26,A) (,-
)
(,-)
(,-)
G
A
H I
E F
CB
D
1325
26
15
-27
-9
6
11
6-9
-3
-4 8
(0,-)
(13,A)
(28,B)
(25,A)
(20,B)
(26,A) (32,G
)
(,-)
(,-)
G
A
H I
E F
CB
D
1325
26
15
-27
-9
6
11
6-9
-3
-4 8
![Page 12: Chapter 13: Graphs](https://reader035.vdocuments.us/reader035/viewer/2022062323/568164b8550346895dd6c4b6/html5/thumbnails/12.jpg)
CS 240 12
Note that this algorithm would not work for graphs with negative-cost cycles. For example, if edge IH in the above example had cost -5 instead of cost -3, then the algorithm would loop indefinitely.
(0,-)
(13,A)
(28,B)
(25,A)
(20,B)
(26,A) (31,F)
(26,F)
(,-)
G
A
H I
E F
CB
D
1325
26
15
-27
-9
6
11
6-9
-3
-4 8
(0,-)
(13,A)
(17,D)
(22,H)
(20,B)
(26,A) (31,F)
(26,F)
(34,D)
G
A
H I
E F
CB
D
1325
26
15
-27
-9
6
11
6-9
-3
-4 8
(0,-)
(13,A)
(17,D)
(22,H)
(20,B)
(26,A) (31,F)
(26,F)
(34,D)
G
A
H I
E F
CB
D
1325
26
15
-27
-9
6
11
6-9
-3
-4 8
(0,-)
(13,A)
(17,D)
(22,H)
(20,B)
(26,A) (31,F)
(26,F)
(34,D)
G
A
H I
E F
CB
D
1325
26
15
-27
-9
6
11
6-9
-3
-4 8
(0,-)
(13,A)
(28,B)
(22,H)
(20,B)
(26,A) (31,F)
(26,F)
(,-)
G
A
H I
E F
CB
D
1325
26
15
-27
-9
6
11
6-9
-3
-4 8
(0,-)
(13,A)
(17,D)
(22,H)
(20,B)
(26,A) (31,F)
(26,F)
(34,D)
G
A
H I
E F
CB
D
1325
26
15
-27
-9
6
11
6-9
-3
-4 8
![Page 13: Chapter 13: Graphs](https://reader035.vdocuments.us/reader035/viewer/2022062323/568164b8550346895dd6c4b6/html5/thumbnails/13.jpg)
CS 240 13
Maximum Flow Problem
Airline Routing
Image Segmentation
When paths have limited capacity, how do you maximize the overall benefit of the routes that are taken?
Network Capacity Planning
![Page 14: Chapter 13: Graphs](https://reader035.vdocuments.us/reader035/viewer/2022062323/568164b8550346895dd6c4b6/html5/thumbnails/14.jpg)
CS 240 14
Maximum Flow AlgorithmAssume that the directed graph G = (V, E) has edge capacities assigned to each edge, and that two vertices s and t have been designated the source and sink nodes, respectively.We wish to maximize the “flow” from s to t by determining how much of each edge’s capacity can be used so that at each vertex, the total incoming flow equals the total outgoing flow.This problem relates to such practical applications as Internet routing and automobile traffic control.
G
A
I
E F
CB
D
H
35
27
18
19
17
21
16
8
12
23
35
15
14 30
12
21
In the graph at left, for instance, the total of the incoming capacities for node C is 40 while the total of the outgoing capacities for node C is 35. Obviously, the maximum flow for this graph will have to “waste” some of the incoming capacity at node C. Conversely, node B’s outgoing capacity exceeds its incoming capacity by 5, so some of its outgoing capacity will have to be wasted.
![Page 15: Chapter 13: Graphs](https://reader035.vdocuments.us/reader035/viewer/2022062323/568164b8550346895dd6c4b6/html5/thumbnails/15.jpg)
CS 240 15
To find a maximum flow, keep track of a flow graph and a residual graph, which keep track of which paths have been added to the flow.Keep choosing paths which yield maximal increases to the flow; add these paths to the flow graph, subtract them from the residual graph, and add their reverse to the residual graph.
Original Graph Flow Graph Residual Graph
G
A
I
E F
CB
D
H
35
27
18
19
17
21
16
8
12
23
35
15
1430
12
21
G
A
I
E F
CB
D
H
0
0
0
0
0
0
0
0
0
0
0
0
00
0
0
G
A
I
E F
CB
D
H
35
27
18
19
17
21
16
8
12
23
35
15
1430
12
21
G
A
I
E F
CB
D
H
35
27
18
19
17
21
16
8
12
23
35
15
1430
12
21
G
A
I
E F
CB
D
H
21
0
0
0
0
21
0
0
0
21
0
0
00
0
0
G
A
I
E F
CB
D
H
14
27
18
19
17
0
16
8
12
2
35
15
1430
12
21
21
21
21
![Page 16: Chapter 13: Graphs](https://reader035.vdocuments.us/reader035/viewer/2022062323/568164b8550346895dd6c4b6/html5/thumbnails/16.jpg)
CS 240 16
Original Graph Flow Graph Residual Graph
G
A
I
E F
CB
D
H
12
18
12
14
10
18
19
0
0
16
8
2
15
1430
4
21
21
211717
17
17
G
A
I
E F
CB
D
H
4
1717
0
10
18
5
0
0
16
8
12
2
15
1430
12
4
3521
21
17
31
14
G
A
I
E F
CB
D
H
0
12
012
0
10
6
5
0
4
8
2
4
3
14 1
8
4
35 2
1
21
17
17
17
31
14
12
12
12
12
23
3527
18
19
17
21
16
8
35
15
14 3
0
21
12
12
G
A
I
E F
CB
D
H
21
17
0
0
17
21
0
0
0
21
17
0
00
0
17
G
A
I
E F
CB
D
H
35
27
18
19
17
21
16
8
12
23
35
15
1430
12
21
G
A
I
E F
CB
D
H
35
17
12
14
17
21
12
0
0
21
31
12
012
12
17
G
A
I
E F
CB
D
H
3527
18
19
17
21
16
8
12
23
35
15
14 3
0
12
21
G
A
I
E F
CB
D
H
35
17
0
14
17
21
0
0
0
21
31
0
00
0
17
G
A
I
E F
CB
D
H
![Page 17: Chapter 13: Graphs](https://reader035.vdocuments.us/reader035/viewer/2022062323/568164b8550346895dd6c4b6/html5/thumbnails/17.jpg)
CS 240 17
Original Graph Flow Graph Residual Graph
G
A
I
E F
CB
D
H
35
27
18
19
17
21
16
8
12
23
35
15
1430
12
21
G
A
I
E F
CB
D
H
35
25
12
14
17
21
12
8
8
21
31
12
620
4
17
G
A
I
E F
CB
D
H
4 4
44
25 4 8
0
2
6
5
00
0
2
3
610
8
35 2
1
21
17
17
31
14
12
12
12
20
8
8
G
A
I
E F
CB
D
H
35
27
18
19
17
21
16
8
12
23
35
15
1430
12
21
G
A
I
E F
CB
D
H
35
25
16
14
17
21
16
8
12
21
31
12
1024
8
17
G
A
I
E F
CB
D
H
04
17
8 12
0
2
2
5
00
0
0
2
4
3
26
4
35 2
1
21
17
25
31
14
16
8
12
24
8
12
Thus, the maximum flow for the graph is 76.
![Page 18: Chapter 13: Graphs](https://reader035.vdocuments.us/reader035/viewer/2022062323/568164b8550346895dd6c4b6/html5/thumbnails/18.jpg)
CS 240 18
Minimum Spanning TreesAn alternative to Kruskal’s Algorithm (which develops a minimum spanning tree via forests) is Prim’s Algorithm, which is just a variation of Dijkstra’s Algorithm. Starting with a minimum-cost edge, it builds the tree by adding minimum-cost edges as long as they don’t create cycles. Like Kruskal’s, Prim’s Algorithm is O(ElogV)
B
A D
C
E
F G
H
B
A D
C
E
F G
H3
B
A D
C
E
F G
H5
4
3
B
A D
C
E
F G
H5
4
6
4
3
3
B
A D
C
E
F G
H5
4
6
3
MINIMUM SPANNING TREE
B
A D
C
E
F G
H5
4
6
4
7
3
3
B
A D
C
E
F G
H4
3
B
A D
C
E
F G
H5
4
6
4
3
ORIGINAL GRAPH
B
A D
C
E
F G
H5
47
6
7 4
7
8
6 93
83
![Page 19: Chapter 13: Graphs](https://reader035.vdocuments.us/reader035/viewer/2022062323/568164b8550346895dd6c4b6/html5/thumbnails/19.jpg)
CS 240 19
Depth-First SearchA convenient means to traverse a graph is to use a depth-first search, which recursively visits and marks the vertices until all of them have been traversed.A B C
F G H
D E
Original Graph
A B C
F G H
D E
Depth-First Search(Solid lines are part of
depth-first spanning tree; dashed lines are visits to
previously marked vertices)
Such a traversal provides a means by which several significant features of a graph can be determined.
A
B
C
F
G
HD
E
Depth-First Spanning Tree
![Page 20: Chapter 13: Graphs](https://reader035.vdocuments.us/reader035/viewer/2022062323/568164b8550346895dd6c4b6/html5/thumbnails/20.jpg)
CS 240 20
Depth-First Search Application: Articulation PointsA vertex v in a graph G is called an articulation point if its removal from G would cause the graph to be disconnected.Such vertices would be considered “critical” in applications like networks, where articulation points are the only means of communication between different portions of the network.A depth-first search can be used to find all of a graph’s articulation points.The only articulation points are the root (if it has more than one child) and any other node v with a child whose “low” number is at least as large as v’s “visit” number. (In this example: nodes B, C, E, and G.)
Original Graph
B E H
C F
GA D J
I
Depth-First Search (with nodes numbered as they’re
visited)
2 6 8
4 5
71 3 10
9
Nodes also marked with lowest-numbered vertex reachable via
zero or more tree edges, followed by at most one back edge
2/1 6/6 8/6
4/1 5/5
7/61/1 3/1 10/7
9/7
![Page 21: Chapter 13: Graphs](https://reader035.vdocuments.us/reader035/viewer/2022062323/568164b8550346895dd6c4b6/html5/thumbnails/21.jpg)
CS 240 21
Depth-First Search Application: Euler CircuitsAn Euler circuit of a graph G is a cycle that visits every edge exactly once.Such a cycle could be useful in applications like networks, where it could provide an efficient means of testing whether each network link is up.A depth-first search can be used to find an Euler circuit of a graph, if one exists. (Note: An Euler circuit exists only if the graph is connected and every vertex has even degree.)
Original Graph
B C D
H I
FA G E
J
Splicing the first two cycles yields cycle A(BCDFB)GHA.Splicing this cycle with the third cycle yields ABCD(FHIF)BGHA.Splicing this cycle with the fourth cycle yields ABCD(FEJF)HIFBGHANote that this traversal takes O(E+V) time.
After Removing First DFS Cycle: BCDFB
B C D
H I
FA G E
JAfter Removing Second
DFS Cycle: ABGHA
B C D
H I
FA G E
J
After Removing Third DFS Cycle: FHIF
B C D
H I
FA G E
JAfter Removing Fourth
DFS Cycle: FEJF
B C D
H I
FA G E
J
![Page 22: Chapter 13: Graphs](https://reader035.vdocuments.us/reader035/viewer/2022062323/568164b8550346895dd6c4b6/html5/thumbnails/22.jpg)
CS 240 22
P and NP ProblemsA problem is said to be a P problem if it can be solved with a deterministic, polynomial-time algorithm. (Deterministic algorithms have each step clearly specified.)
Example: The Knapsack ProblemGiven a set of n valuable jewels J1, J2, …, Jn with respective weights w1, w2, …, wn, and respective prices p1, p2, …, pn, as well as a knapsack capable of supporting a total weight of M.
A nondeterministic polynomial-time solution:totalWorth = 0;totalWeight = 0;for (i=1; i<=n; i++){ b[i] = choice(TRUE,FALSE); if (b[i]==TRUE) { totalWorth += p[i]; totalWeight += w[i]; }}if ((totalWorth >= T) && (totalWeight <= M)) cout << “YAHOO! I’M RICH!”;else cout << “@#$&%!”;
A problem is said to be an NP problem if it can be solved with a nondeterministic, polynomial-time algorithm. In essence, at a critical point in the algorithm, a decision must be made, and it is assumed that a magical “choice” function always chooses correctly.
Problem: Is there a way to pack at least T dollars worth of jewels, without exceeding the weight capacity of the knapsack?(It’s not as easy as it sounds; three lightweight $1000 jewels might be preferable to one heavy $2500 jewel, for instance.)