+ All Categories
Home > Documents > Chapter 13 Graphs

Chapter 13 Graphs

Date post: 18-Jan-2016
Category:
Upload: essien
View: 33 times
Download: 1 times
Share this document with a friend
Description:
Chapter 13 Graphs. The graph ADT, while relatively unstructured, provides a platform for solving some of the most sophisticated problems in computer science. Graph Abstract Data Types. Topological Sort. Minimum Path. Maximum Flow. Spanning Tree. Depth-First Search. 4. 3. 6. 6. 5. - PowerPoint PPT Presentation
20
CS CS 240 Chapter Chapter 13 - Graphs - Graphs Page 1 Chapter 13 Chapter 13 Graphs Graphs The graph ADT, while relatively unstructured, provides The graph ADT, while relatively unstructured, provides a platform for solving some of the most sophisticated a platform for solving some of the most sophisticated problems in computer science. problems in computer science. Graph Abstract Data Types Graph Abstract Data Types Spanning Tree Spanning Tree Topological Sort Topological Sort Depth-First Search Depth-First Search Minimum Path Minimum Path Maximum Flow Maximum Flow
Transcript
Page 1: Chapter 13 Graphs

CS CS 240 Chapter Chapter 13 - Graphs - Graphs Page 1

Chapter 13Chapter 13GraphsGraphs

The graph ADT, while relatively unstructured, The graph ADT, while relatively unstructured, provides a platform for solving some of the provides a platform for solving some of the most sophisticated problems in computer most sophisticated problems in computer science.science. Graph Abstract Data TypesGraph Abstract Data Types

Spanning TreeSpanning Tree

Topological SortTopological Sort

Depth-First SearchDepth-First Search

Minimum PathMinimum Path Maximum FlowMaximum Flow

Page 2: Chapter 13 Graphs

CS CS 240 Chapter Chapter 13 - Graphs - Graphs Page 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)

34

2

4

2

3

5

6

3

65

Page 3: Chapter 13 Graphs

CS CS 240 Chapter Chapter 13 - Graphs - Graphs Page 3

Graph RepresentationsAdjacency Matrix:

AB

D

H

E

G

F

C

ABCDEFGH

A B C D E F G H11000010

10110000

01000000

01000000

00001010

00000010

10001101

00000010

AB

D

H

E

G

F

C

ABCDEFGH

A B C D E F G H00000100

10100000

00001000

11000000

00010001

00010000

00010100

00000010

AB

D

H

E

G

F

C33

44

22

44

22

33

55

66

33

6655

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

CS CS 240 Chapter Chapter 13 - Graphs - Graphs Page 4

Adjacency List:

A

B

C

D

E

F

G

H

AA

AA

BB

BB

EE

GG

AA

GG

BB

CC

GG

EE

GG

DD

FF HH

AABB

DD

HH

EE

GG

FF

CC

A

B

C

D

E

F

G

H

BB

BB 33

HH

GG

66

22

44

CC

AA

33

55

33

55

44

22

DD

EE

GG

GG

66EE

AABB

DD

HH

EE

GG

FF

CC33

44

22

44

22

33

55

66

33

6655

A

B

C

D

E

F

G

H

BB

EE

BB

GG

CC

AA

EE

DD

FF

GG

DD

HH

AABB

DD

HH

EE

GG

FF

CC

Page 5: Chapter 13 Graphs

CS CS 240 Chapter Chapter 13 - Graphs - Graphs Page 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, MATH 150,CS 140, MATH 223,CS 150, CS 240,MATH 152, CS 275,CS 312, CS 321,CS 250, CS 314,CS 425, ECE 382,CS 434, ECE 483,CS 330, STAT 380,CS 407, CS 423,CS 416, CS 325,CS 438, CS 454,CS 447, CS 499,CS 482, CS 456,CS 444, ECE 481, ECE 482

MATH 120

CS 140

CS 150MATH 152

CS 240 CS 275MATH 223

CS 250

CS 407

STAT 380

CS 438

CS 454

CS 456

CS 325

CS 499

CS 434CS 312CS 482

CS 330

CS 423

CS 416

CS 444

CS 447

MATH 150

CS 314

CS 425

CS 321

ECE 483

ECE 382

ECE 481

ECE 482

Page 6: Chapter 13 Graphs

CS CS 240 Chapter Chapter 13 - Graphs - Graphs Page 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.Output an element v in the indegree-zero list.– For each vertex w with (v,w) in the edge set E:For each vertex w with (v,w) in the edge set E:

Decrement the indegree of w by one.Decrement the indegree of w by one. Place w in the indegree-zero list if its new indegree is 0.Place w in the indegree-zero list if its new indegree is 0.

G

A

H I

E F D

CB

0011331111441133

--00331100440033

--002211--330022

--002200--220011

----1100--220011

----00----110011

----00----11--00

----------11--00

00 00 00 -- -- -- -- --

Indegree of Indegree of A:A:Indegree of Indegree of B:B:Indegree of Indegree of C:C:Indegree of Indegree of D:D:Indegree of Indegree of E:E:Indegree of Indegree of F:F:Indegree of Indegree of G:G:Indegree of Indegree of H:H:Indegree of I:Indegree of I:

Output:Output: AA EE II BB DD GG CC HH

----------00------

FF

------------------

Page 7: Chapter 13 Graphs

CS CS 240 Chapter Chapter 13 - Graphs - Graphs Page 7

Shortest-Path AlgorithmsHow do you find the shortest path from a specified vertex to every

other vertex in the directed graph?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).

G

A

H I

E F

CB

D(0,-)(0,-)

(0,-)(0,-)

(1,A)(1,A)

(1,A)(1,A)

(1,A)(1,A)

(2,B)(2,B)

(2,E)(2,E)

(2,E)(2,E)

(3,F)(3,F)

G

A

H I

E F

CB

D

(0,-)(0,-)

(1,A)(1,A)

(1,A)(1,A)

(1,A)(1,A)G

A

H I

E F

CB

D (0,-)(0,-)

(1,A)(1,A)

(1,A)(1,A)

(1,A)(1,A)

(2,B)(2,B)

(2,E)(2,E)

(2,E)(2,E)

G

A

H I

E F

CB

D

(0,-)(0,-)

(1,A)(1,A)

(1,A)(1,A)

(1,A)(1,A)

(2,B)(2,B)

(2,E)(2,E)

(2,E)(2,E)

(3,F)(3,F)

(4,D)(4,D)G

A

H I

E F

CB

D

Page 8: Chapter 13 Graphs

CS CS 240 Chapter Chapter 13 - Graphs - Graphs Page 8

Shortest-Path Algorithms (Continued)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).

GG

AA

HH II

EE FF

CCBB

DD13131313

7777

6666

8888

19191919

21212121

16161616

8888

2222

2222

5555

3333

4444 3333

(0,-)(0,-)(0,-)(0,-)

((,-),-)((,-),-) ((,-),-)((,-),-)

((,-),-)((,-),-) ((,-),-)((,-),-)

((,-),-)((,-),-)((,-),-)((,-),-)

((,-),-)((,-),-)

((,-),-)((,-),-)

(0,-)(0,-)(0,-)(0,-)

(7,A)(7,A)(7,A)(7,A) (26,B)(26,B)(26,B)(26,B)

(6,A)(6,A)(6,A)(6,A) (19,E)(19,E)(19,E)(19,E)

(8,A)(8,A)(8,A)(8,A)(22,E)(22,E)(22,E)(22,E)

((,-),-)((,-),-)

((,-),-)((,-),-)GG

AA

HH II

EE FF

CCBB

DD13131313

7777

6666

8888

19191919

21212121

16161616

8888

2222

2222

5555

3333

4444 3333

(0,-)(0,-)(0,-)(0,-)

(7,A)(7,A)(7,A)(7,A) (27,E)(27,E)(27,E)(27,E)

(6,A)(6,A)(6,A)(6,A) (19,E)(19,E)(19,E)(19,E)

(8,A)(8,A)(8,A)(8,A)(22,E)(22,E)(22,E)(22,E)

((,-),-)((,-),-)

((,-),-)((,-),-)GG

AA

HH II

EE FF

CCBB

DD13131313

7777

6666

8888

19191919

21212121

16161616

8888

2222

2222

5555

3333

4444 3333

(0,-)(0,-)(0,-)(0,-)

(7,A)(7,A)(7,A)(7,A) (26,B)(26,B)(26,B)(26,B)

(6,A)(6,A)(6,A)(6,A) (18,H)(18,H)(18,H)(18,H)

(8,A)(8,A)(8,A)(8,A)(16,G)(16,G)(16,G)(16,G)

((,-),-)((,-),-)

((,-),-)((,-),-)GG

AA

HH II

EE FF

CCBB

DD13131313

7777

6666

8888

19191919

21212121

16161616

8888

2222

2222

5555

3333

4444 3333

(0,-)(0,-)(0,-)(0,-)

(7,A)(7,A)(7,A)(7,A) ((,-),-)((,-),-)

(6,A)(6,A)(6,A)(6,A) ((,-),-)((,-),-)

(8,A)(8,A)(8,A)(8,A)((,-),-)((,-),-)

((,-),-)((,-),-)

((,-),-)((,-),-)GG

AA

HH II

EE FF

CCBB

DD13131313

7777

6666

8888

19191919

21212121

16161616

8888

2222

2222

5555

3333

4444 3333

(0,-)(0,-)(0,-)(0,-)

(7,A)(7,A)(7,A)(7,A) (26,B)(26,B)(26,B)(26,B)

(6,A)(6,A)(6,A)(6,A) (19,E)(19,E)(19,E)(19,E)

(8,A)(8,A)(8,A)(8,A)(16,G)(16,G)(16,G)(16,G)

((,-),-)((,-),-)

((,-),-)((,-),-)GG

AA

HH II

EE FF

CCBB

DD13131313

7777

6666

8888

19191919

21212121

16161616

8888

2222

2222

5555

3333

4444 3333

Page 9: Chapter 13 Graphs

CS CS 240 Chapter Chapter 13 - Graphs - Graphs Page 9

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,-)(0,-)(0,-)(0,-)

(7,A)(7,A)(7,A)(7,A) (25,D)(25,D)(25,D)(25,D)

(6,A)(6,A)(6,A)(6,A) (18,H)(18,H)(18,H)(18,H)

(8,A)(8,A)(8,A)(8,A)(16,G)(16,G)(16,G)(16,G)

(20,F)(20,F)(20,F)(20,F)

(23,D)(23,D)(23,D)(23,D)GG

AA

HH II

EE FF

CCBB

DD13131313

7777

6666

8888

19191919

21212121

16161616

8888

2222

2222

5555

3333

4444 3333

(0,-)(0,-)(0,-)(0,-)

(7,A)(7,A)(7,A)(7,A) (26,B)(26,B)(26,B)(26,B)

(6,A)(6,A)(6,A)(6,A) (18,H)(18,H)(18,H)(18,H)

(8,A)(8,A)(8,A)(8,A)(16,G)(16,G)(16,G)(16,G)

(20,F)(20,F)(20,F)(20,F)

((,-),-)((,-),-)GG

AA

HH II

EE FF

CCBB

DD13131313

7777

6666

8888

19191919

21212121

16161616

8888

2222

2222

5555

3333

4444 3333

(0,-)(0,-)(0,-)(0,-)

(7,A)(7,A)(7,A)(7,A) (25,D)(25,D)(25,D)(25,D)

(6,A)(6,A)(6,A)(6,A) (18,H)(18,H)(18,H)(18,H)

(8,A)(8,A)(8,A)(8,A)(16,G)(16,G)(16,G)(16,G)

(20,F)(20,F)(20,F)(20,F)

(23,D)(23,D)(23,D)(23,D)GG

AA

HH II

EE FF

CCBB

DD13131313

7777

6666

8888

19191919

21212121

16161616

8888

2222

2222

5555

3333

4444 3333

(0,-)(0,-)(0,-)(0,-)

(7,A)(7,A)(7,A)(7,A) (25,D)(25,D)(25,D)(25,D)

(6,A)(6,A)(6,A)(6,A) (18,H)(18,H)(18,H)(18,H)

(8,A)(8,A)(8,A)(8,A)(16,G)(16,G)(16,G)(16,G)

(20,F)(20,F)(20,F)(20,F)

(23,D)(23,D)(23,D)(23,D)GG

AA

HH II

EE FF

CCBB

DD13131313

7777

6666

8888

19191919

21212121

16161616

8888

2222

2222

5555

3333

4444 3333

Page 10: Chapter 13 Graphs

CS CS 240 Chapter Chapter 13 - Graphs - Graphs Page 10

Shortest-Path Algorithms (Continued)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).

GG

AA

HH II

EE FF

CCBB

DD

13131313

25252525

26262626

15151515

-2-2-2-2

7777

-9-9-9-9

6666

11111111

6666

-9-9-9-9

-3-3-3-3

-4-4-4-4 8888

(0,-)(0,-)(0,-)(0,-)

((,-),-)((,-),-) ((,-),-)((,-),-)

((,-),-)((,-),-) ((,-),-)((,-),-)

((,-),-)((,-),-)((,-),-)((,-),-)

((,-),-)((,-),-)

((,-),-)((,-),-)

(0,-)(0,-)(0,-)(0,-)

(13,A)(13,A)(13,A)(13,A) (28,B)(28,B)(28,B)(28,B)

(25,A)(25,A)(25,A)(25,A) (20,B)(20,B)(20,B)(20,B)

(26,A)(26,A)(26,A)(26,A)((,-),-)((,-),-)

((,-),-)((,-),-)

((,-),-)((,-),-)GG

AA

HH II

EE FF

CCBB

DD

13131313

25252525

26262626

15151515

-2-2-2-2

7777

-9-9-9-9

6666

11111111

6666

-9-9-9-9

-3-3-3-3

-4-4-4-4 8888

(0,-)(0,-)(0,-)(0,-)

(13,A)(13,A)(13,A)(13,A) ((,-),-)((,-),-)

(25,A)(25,A)(25,A)(25,A) (23,E)(23,E)(23,E)(23,E)

(26,A)(26,A)(26,A)(26,A)((,-),-)((,-),-)

((,-),-)((,-),-)

((,-),-)((,-),-)GG

AA

HH II

EE FF

CCBB

DD

13131313

25252525

26262626

15151515

-2-2-2-2

7777

-9-9-9-9

6666

11111111

6666

-9-9-9-9

-3-3-3-3

-4-4-4-4 8888

(0,-)(0,-)(0,-)(0,-)

(13,A)(13,A)(13,A)(13,A) (28,B)(28,B)(28,B)(28,B)

(25,A)(25,A)(25,A)(25,A) (20,B)(20,B)(20,B)(20,B)

(26,A)(26,A)(26,A)(26,A)(31,F)(31,F)(31,F)(31,F)

(26,F)(26,F)(26,F)(26,F)

((,-),-)((,-),-)GG

AA

HH II

EE FF

CCBB

DD

13131313

25252525

26262626

15151515

-2-2-2-2

7777

-9-9-9-9

6666

11111111

6666

-9-9-9-9

-3-3-3-3

-4-4-4-4 8888

(0,-)(0,-)(0,-)(0,-)

(13,A)(13,A)(13,A)(13,A) ((,-),-)((,-),-)

(25,A)(25,A)(25,A)(25,A) ((,-),-)((,-),-)

(26,A)(26,A)(26,A)(26,A)((,-),-)((,-),-)

((,-),-)((,-),-)

((,-),-)((,-),-)GG

AA

HH II

EE FF

CCBB

DD

13131313

25252525

26262626

15151515

-2-2-2-2

7777

-9-9-9-9

6666

11111111

6666

-9-9-9-9

-3-3-3-3

-4-4-4-4 8888

(0,-)(0,-)(0,-)(0,-)

(13,A)(13,A)(13,A)(13,A) (28,B)(28,B)(28,B)(28,B)

(25,A)(25,A)(25,A)(25,A) (20,B)(20,B)(20,B)(20,B)

(26,A)(26,A)(26,A)(26,A)(32,G)(32,G)(32,G)(32,G)

((,-),-)((,-),-)

((,-),-)((,-),-)GG

AA

HH II

EE FF

CCBB

DD

13131313

25252525

26262626

15151515

-2-2-2-2

7777

-9-9-9-9

6666

11111111

6666

-9-9-9-9

-3-3-3-3

-4-4-4-4 8888

Page 11: Chapter 13 Graphs

CS CS 240 Chapter Chapter 13 - Graphs - Graphs Page 11

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,-)(0,-)(0,-)(0,-)

(13,A)(13,A)(13,A)(13,A) (28,B)(28,B)(28,B)(28,B)

(25,A)(25,A)(25,A)(25,A) (20,B)(20,B)(20,B)(20,B)

(26,A)(26,A)(26,A)(26,A)(31,F)(31,F)(31,F)(31,F)

(26,F)(26,F)(26,F)(26,F)

((,-),-)((,-),-)GG

AA

HH II

EE FF

CCBB

DD

13131313

25252525

26262626

15151515

-2-2-2-2

7777

-9-9-9-9

6666

11111111

6666

-9-9-9-9

-3-3-3-3

-4-4-4-4 8888

(0,-)(0,-)(0,-)(0,-)

(13,A)(13,A)(13,A)(13,A) (17,D)(17,D)(17,D)(17,D)

(22,H)(22,H)(22,H)(22,H) (20,B)(20,B)(20,B)(20,B)

(26,A)(26,A)(26,A)(26,A)(31,F)(31,F)(31,F)(31,F)

(26,F)(26,F)(26,F)(26,F)

(34,D)(34,D)(34,D)(34,D)GG

AA

HH II

EE FF

CCBB

DD

13131313

25252525

26262626

15151515

-2-2-2-2

7777

-9-9-9-9

6666

11111111

6666

-9-9-9-9

-3-3-3-3

-4-4-4-4 8888

(0,-)(0,-)(0,-)(0,-)

(13,A)(13,A)(13,A)(13,A) (17,D)(17,D)(17,D)(17,D)

(22,H)(22,H)(22,H)(22,H) (20,B)(20,B)(20,B)(20,B)

(26,A)(26,A)(26,A)(26,A)(31,F)(31,F)(31,F)(31,F)

(26,F)(26,F)(26,F)(26,F)

(34,D)(34,D)(34,D)(34,D)GG

AA

HH II

EE FF

CCBB

DD

13131313

25252525

26262626

15151515

-2-2-2-2

7777

-9-9-9-9

6666

11111111

6666

-9-9-9-9

-3-3-3-3

-4-4-4-4 8888

(0,-)(0,-)(0,-)(0,-)

(13,A)(13,A)(13,A)(13,A) (17,D)(17,D)(17,D)(17,D)

(22,H)(22,H)(22,H)(22,H) (20,B)(20,B)(20,B)(20,B)

(26,A)(26,A)(26,A)(26,A)(31,F)(31,F)(31,F)(31,F)

(26,F)(26,F)(26,F)(26,F)

(34,D)(34,D)(34,D)(34,D)GG

AA

HH II

EE FF

CCBB

DD

13131313

25252525

26262626

15151515

-2-2-2-2

7777

-9-9-9-9

6666

11111111

6666

-9-9-9-9

-3-3-3-3

-4-4-4-4 8888

(0,-)(0,-)(0,-)(0,-)

(13,A)(13,A)(13,A)(13,A) (28,B)(28,B)(28,B)(28,B)

(22,H)(22,H)(22,H)(22,H) (20,B)(20,B)(20,B)(20,B)

(26,A)(26,A)(26,A)(26,A)(31,F)(31,F)(31,F)(31,F)

(26,F)(26,F)(26,F)(26,F)

((,-),-)((,-),-)GG

AA

HH II

EE FF

CCBB

DD

13131313

25252525

26262626

15151515

-2-2-2-2

7777

-9-9-9-9

6666

11111111

6666

-9-9-9-9

-3-3-3-3

-4-4-4-4 8888

(0,-)(0,-)(0,-)(0,-)

(13,A)(13,A)(13,A)(13,A) (17,D)(17,D)(17,D)(17,D)

(22,H)(22,H)(22,H)(22,H) (20,B)(20,B)(20,B)(20,B)

(26,A)(26,A)(26,A)(26,A)(31,F)(31,F)(31,F)(31,F)

(26,F)(26,F)(26,F)(26,F)

(34,D)(34,D)(34,D)(34,D)GG

AA

HH II

EE FF

CCBB

DD

13131313

25252525

26262626

15151515

-2-2-2-2

7777

-9-9-9-9

6666

11111111

6666

-9-9-9-9

-3-3-3-3

-4-4-4-4 8888

Page 12: Chapter 13 Graphs

CS CS 240 Chapter Chapter 13 - Graphs - Graphs Page 12

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.

GG

AA

II

EE FF

CCBB

DD

HH

35353535

27272727

18181818

19191919

17171717

21212121

16161616

8888

12121212

23232323

35353535

15151515

14141414

3030303012121212

21212121

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 13: Chapter 13 Graphs

CS CS 240 Chapter Chapter 13 - Graphs - Graphs Page 13

Maximum-Flow Algorithm (Continued)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 GraphOriginal Graph Flow GraphFlow Graph Residual GraphResidual Graph

GG

AA

II

EE FF

CCBB

DD

HH

35353535

27272727

18181818

19191919

17171717

21212121

16161616

8888

12121212

23232323

35353535

15151515

14141414

3030303012121212

21212121

GG

AA

II

EE FF

CCBB

DD

HH

0000

0000

0000

0000

0000

0000

0000

0000

0000

0000

0000

0000

0000

00000000

0000

GG

AA

II

EE FF

CCBB

DD

HH

35353535

27272727

18181818

19191919

17171717

21212121

16161616

8888

12121212

23232323

35353535

15151515

14141414

3030303012121212

21212121

GG

AA

II

EE FF

CCBB

DD

HH

35353535

27272727

18181818

19191919

17171717

21212121

16161616

8888

12121212

23232323

35353535

15151515

14141414

3030303012121212

21212121

GG

AA

II

EE FF

CCBB

DD

HH

21212121

0000

0000

0000

0000

21212121

0000

0000

0000

21212121

0000

0000

0000

00000000

0000

GG

AA

II

EE FF

CCBB

DD

HH

14141414

27272727

18181818

19191919

17171717

0000

16161616

8888

12121212

2222

35353535

15151515

14141414

3030303012121212

21212121

21212121

21212121

21212121

Page 14: Chapter 13 Graphs

CS CS 240 Chapter Chapter 13 - Graphs - Graphs Page 14

Original GraphOriginal Graph Flow GraphFlow Graph Residual GraphResidual Graph

GG

AA

II

EE FF

CCBB

DD

HH

12121212

18181818

12121212

14141414

10101010

18181818

19191919

0000

0000

16161616

8888

2222

15151515

14141414

30303030

4444

21212121

21212121

212121211717171717171717

17171717

17171717

GG

AA

II

EE FF

CCBB

DD

HH

4444

1717171717171717

0000

10101010

18181818

5555

0000

0000

16161616

8888

12121212

2222

15151515

14141414

3030303012121212

4444

35353535

21212121

21212121

17171717

31313131

14141414

GG

AA

II

EE FF

CCBB

DD

HH

0000

121212120000

12121212

0000

10101010

6666

5555

0000

4444

8888

2222

4444

3333

14141414

18181818

4444

35353535

21212121

212121211717171717171717

17171717

31313131

14141414

12121212 12121212

12121212

12121212

23232323

35353535

27272727

18181818

19191919

17171717

21212121

16161616

8888

12121212

35353535

15151515

14141414

3030303012121212

21212121

GG

AA

II

EE FF

CCBB

DD

HH

21212121

17171717

0000

0000

17171717

21212121

0000

0000

0000

21212121

17171717

0000

0000

00000000

17171717

GG

AA

II

EE FF

CCBB

DD

HH

35353535

27272727

18181818

19191919

17171717

21212121

16161616

8888

12121212

23232323

35353535

15151515

14141414

3030303012121212

21212121

GG

AA

II

EE FF

CCBB

DD

HH

35353535

17171717

12121212

14141414

17171717

21212121

12121212

0000

0000

21212121

31313131

12121212

0000

1212121212121212

17171717

GG

AA

II

EE FF

CCBB

DD

HH

35353535

27272727

18181818

19191919

17171717

21212121

16161616

8888

12121212

23232323

35353535

15151515

14141414

3030303012121212

21212121

GG

AA

II

EE FF

CCBB

DD

HH

35353535

17171717

0000

14141414

17171717

21212121

0000

0000

0000

21212121

31313131

0000

0000

00000000

17171717

GG

AA

II

EE FF

CCBB

DD

HH

Page 15: Chapter 13 Graphs

CS CS 240 Chapter Chapter 13 - Graphs - Graphs Page 15

Original GraphOriginal Graph Flow GraphFlow Graph Residual GraphResidual Graph

GG

AA

II

EE FF

CCBB

DD

HH

35353535

27272727

18181818

19191919

17171717

21212121

16161616

8888

12121212

23232323

35353535

15151515

14141414

3030303012121212

21212121

GG

AA

II

EE FF

CCBB

DD

HH

35353535

25252525

12121212

14141414

17171717

21212121

12121212

8888

8888

21212121

31313131

12121212

6666

202020204444

17171717

GG

AA

II

EE FF

CCBB

DD

HH

44444444

44444444

25252525

4444 8888

0000

2222

6666

5555

0000

0000

0000

2222

3333

6666

101010108888

35353535

21212121

2121212117171717

17171717

31313131

14141414

1212121212121212

12121212

20202020

8888

8888

GG

AA

II

EE FF

CCBB

DD

HH

35353535

27272727

18181818

19191919

17171717

21212121

16161616

8888

12121212

23232323

35353535

15151515

14141414

3030303012121212

21212121

GG

AA

II

EE FF

CCBB

DD

HH

35353535

25252525

16161616

14141414

17171717

21212121

16161616

8888

12121212

21212121

31313131

12121212

10101010

242424248888

17171717

GG

AA

II

EE FF

CCBB

DD

HH

00004444

17171717

888812121212

0000

2222

2222

5555

0000

0000

0000

0000

2222

4444

3333

2222

6666

4444

35353535

21212121

212121211717171725252525

31313131

14141414

16161616 8888

12121212

24242424

8888

12121212

Thus, the maximum Thus, the maximum flow for the graph is flow for the graph is

76.76.

Page 16: Chapter 13 Graphs

CS CS 240 Chapter Chapter 13 - Graphs - Graphs Page 16

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

H33

B

A D

C

E

F G

H

55

44

33

B

A D

C

E

F G

H

55

44

66

44

33

33

B

A D

C

E

F G

H

55

44

66

33

MINIMUM SPANNING TREEMINIMUM SPANNING TREE

B

A D

C

E

F G

H

55

44

66

44

77

33

33

B

A D

C

E

F G

H

44

33

B

A D

C

E

F G

H

55

44

66

44

33

ORIGINAL GRAPHORIGINAL GRAPH

B

A D

C

E

F G

H

55

44

77

66

77 44

77

88

66 9933

8833

Page 17: Chapter 13 Graphs

CS CS 240 Chapter Chapter 13 - Graphs - Graphs Page 17

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.

AA BB CC

FF GG HH

DD EE

Original GraphOriginal Graph

AA BB CC

FF GG HH

DD EE

Depth-First SearchDepth-First Search

(Solid lines are part of (Solid lines are part of depth-first spanning tree; depth-first spanning tree; dashed lines are visits to dashed lines are visits to

previously marked previously marked vertices)vertices)

Such a traversal provides a means by which several significant features of a graph can be determined.

AA

BB

CC

FF

GG

HHDD

EE

Depth-First Depth-First Spanning TreeSpanning Tree

Page 18: Chapter 13 Graphs

CS CS 240 Chapter Chapter 13 - Graphs - Graphs Page 18

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 GraphOriginal Graph

BB EE HH

CC FF

GGAA DD JJ

II

Depth-First Search Depth-First Search (with nodes (with nodes

numbered as they’re numbered as they’re visited)visited)

22 66 88

44 55

7711 33 1010

99

Nodes also marked with lowest-Nodes also marked with lowest-numbered vertex reachable via numbered vertex reachable via

zero or more tree edges, zero or more tree edges, followed by at most one back followed by at most one back

edgeedge

2/12/1 6/66/6 8/68/6

4/14/1 5/55/5

7/67/61/11/1 3/13/1 10/710/7

9/79/7

Page 19: Chapter 13 Graphs

CS CS 240 Chapter Chapter 13 - Graphs - Graphs Page 19

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 GraphOriginal Graph

BB CC DD

HH II

FFAA GG EE

JJ

Splicing the first two cycles yields cycle A(BCDFB)GHA.

Splicing this cycle with the third cycle yields ABCD(FEJF)BGHA.

Splicing this cycle with the fourth cycle yields ABCD(FHIF)EJFBGHA

Note that this traversal takes O(E+V) time.

After Removing First After Removing First DFS Cycle: BCDFBDFS Cycle: BCDFB

BB CC DD

HH II

FFAA GG EE

JJ

After Removing Second After Removing Second DFS Cycle: ABGHADFS Cycle: ABGHA

BB CC DD

HH II

FFAA GG EE

JJ

After Removing Third After Removing Third DFS Cycle: FEJFDFS Cycle: FEJF

BB CC DD

HH II

FFAA GG EE

JJ

After Removing After Removing Fourth DFS Cycle: Fourth DFS Cycle:

FHIFFHIF

BB CC DD

HH II

FFAA GG EE

JJ

Page 20: Chapter 13 Graphs

CS CS 240 Chapter Chapter 13 - Graphs - Graphs Page 20

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.)


Recommended