chapter 6 graphs. 2 outline definitions, terminologies and applications graph representation...

109
Chapter 6 Graphs

Upload: homer-weaver

Post on 08-Jan-2018

223 views

Category:

Documents


0 download

DESCRIPTION

3 Graphs G = (V,E) V is the vertex set. Vertices are also called nodes and points. E is the edge set. Each edge connects two different vertices. Undirected edge has no orientation (u,v). Directed edge has an orientation (u,v). uv uv

TRANSCRIPT

Page 1: Chapter 6 Graphs. 2 Outline Definitions, Terminologies and Applications Graph Representation Elementary graph operations Famous Graph Problems

Chapter 6Graphs

Page 2: Chapter 6 Graphs. 2 Outline Definitions, Terminologies and Applications Graph Representation Elementary graph operations Famous Graph Problems

2

Outline

Definitions, Terminologies and Applications Graph Representation Elementary graph operations Famous Graph Problems

Page 3: Chapter 6 Graphs. 2 Outline Definitions, Terminologies and Applications Graph Representation Elementary graph operations Famous Graph Problems

3

Graphs G = (V,E) V is the vertex set. Vertices are also called nodes and points. E is the edge set. Each edge connects two different vertices. Undirected edge has no orientation (u,v).

Directed edge has an orientation (u,v).u v

u v

Page 4: Chapter 6 Graphs. 2 Outline Definitions, Terminologies and Applications Graph Representation Elementary graph operations Famous Graph Problems

4

Graph

Undirected graph => no oriented edge Directed graph => every edge has an

orientation

Proper graph No loops No multi-edges

Page 5: Chapter 6 Graphs. 2 Outline Definitions, Terminologies and Applications Graph Representation Elementary graph operations Famous Graph Problems

5

Undirected Graph

23

8101

45 9 11

67

Page 6: Chapter 6 Graphs. 2 Outline Definitions, Terminologies and Applications Graph Representation Elementary graph operations Famous Graph Problems

6

Directed Graph (Digraph)

23

8101

45 9 11

67

Page 7: Chapter 6 Graphs. 2 Outline Definitions, Terminologies and Applications Graph Representation Elementary graph operations Famous Graph Problems

7

Examples of Graphlike Structures

0

2

1 1

2

3

0

(a) Graph with a self edge

(b) Multigraph

Page 8: Chapter 6 Graphs. 2 Outline Definitions, Terminologies and Applications Graph Representation Elementary graph operations Famous Graph Problems

8

Applications—Communication Network

Vertex = city, edge = communication link.

23

8101

45 9 11

67

Page 9: Chapter 6 Graphs. 2 Outline Definitions, Terminologies and Applications Graph Representation Elementary graph operations Famous Graph Problems

9

Driving Distance/Time Map

Vertex = city, edge weight = driving distance/time. Weighted undirected graph

23

8101

45 9 11

67

48

6

6

7

5

24

4 53

Page 10: Chapter 6 Graphs. 2 Outline Definitions, Terminologies and Applications Graph Representation Elementary graph operations Famous Graph Problems

10

Street Map

Some streets are one way.

23

8101

45 9 11

67

Page 11: Chapter 6 Graphs. 2 Outline Definitions, Terminologies and Applications Graph Representation Elementary graph operations Famous Graph Problems

11

Terminologies

Vertex, edge, directed, undirected, weighted, unweighted

Adjacenct Subgraph Path

A path from u to v in graph G is a sequence of vertices u,i1, i2,…,jk, v such that (u, i1)…(ik, v) are edges in E(G).

The length of a path is the number of edges on it. Simple path

All vertices except possibly the first and last are distinct Cycle

A simple path whose first and last vertices are the same

Page 12: Chapter 6 Graphs. 2 Outline Definitions, Terminologies and Applications Graph Representation Elementary graph operations Famous Graph Problems

12

G1 and G3 Subgraphs

0

3

1 20

3

1 20

1 2

0 0

1

2

0

1

2

0

1

2

(a) Some subgraphs of G1

(a) Some subgraphs of G3

(i) (ii) (iii) (iv)

(i) (ii)

(iii) (iv)

Page 13: Chapter 6 Graphs. 2 Outline Definitions, Terminologies and Applications Graph Representation Elementary graph operations Famous Graph Problems

13

Terminologies

Undirected graph Connected

Vertices Graph

Connected component Maximal connected subgraph

degree

Page 14: Chapter 6 Graphs. 2 Outline Definitions, Terminologies and Applications Graph Representation Elementary graph operations Famous Graph Problems

14

Terminologies

Directed graph Strongly connected Strongly connected component

A maximal subgraph that is strongly connected indegree outdegree

Acyclic Cycle free graph

Page 15: Chapter 6 Graphs. 2 Outline Definitions, Terminologies and Applications Graph Representation Elementary graph operations Famous Graph Problems

15

Complete Undirected Graph

Has all possible edges.

n = 1 n = 2 n = 3 n = 4

Page 16: Chapter 6 Graphs. 2 Outline Definitions, Terminologies and Applications Graph Representation Elementary graph operations Famous Graph Problems

16

Number Of Edges—Undirected Graph

Each edge is of the form (u,v), u != v. Number of such pairs in an n vertex graph is

n(n-1). Since edge (u,v) is the same as edge (v,u),

the number of edges in a complete undirected graph is n(n-1)/2.

Number of edges in an undirected graph is <= n(n-1)/2.

Page 17: Chapter 6 Graphs. 2 Outline Definitions, Terminologies and Applications Graph Representation Elementary graph operations Famous Graph Problems

17

Number Of Edges—Directed Graph Each edge is of the form (u,v), u != v. Number of such pairs in an n vertex graph is

n(n-1). Since edge (u,v) is not the same as edge

(v,u), the number of edges in a complete directed graph is n(n-1).

Number of edges in a directed graph is <= n(n-1).

Page 18: Chapter 6 Graphs. 2 Outline Definitions, Terminologies and Applications Graph Representation Elementary graph operations Famous Graph Problems

18

Sum Of Vertex Degrees

Sum of degrees = 2e (e is number of edges)

810

9 11

Page 19: Chapter 6 Graphs. 2 Outline Definitions, Terminologies and Applications Graph Representation Elementary graph operations Famous Graph Problems

19

Sum Of In- And Out-Degrees

each edge contributes 1 to the in-degree of some vertex and 1 to the out-degree of some other vertex

sum of in-degrees = sum of out-degrees = e,where e is the number of edges in the

digraph

Page 20: Chapter 6 Graphs. 2 Outline Definitions, Terminologies and Applications Graph Representation Elementary graph operations Famous Graph Problems

20

Graph Representation Adjacency Matrix Adjacency Lists

Page 21: Chapter 6 Graphs. 2 Outline Definitions, Terminologies and Applications Graph Representation Elementary graph operations Famous Graph Problems

21

Adjacency Matrix 0/1 n x n matrix, where n = # of vertices A(i,j) = 1 iff (i,j) is an edge

23

1

45

1 2 3 4 512345

0 1 0 1 01 0 0 0 10 0 0 0 11 0 0 0 10 1 1 1 0

Page 22: Chapter 6 Graphs. 2 Outline Definitions, Terminologies and Applications Graph Representation Elementary graph operations Famous Graph Problems

22

Adjacency Matrix Properties

23

1

45

1 2 3 4 512345

0 1 0 1 01 0 0 0 10 0 0 0 11 0 0 0 10 1 1 1 0

•Diagonal entries are zero.

•Adjacency matrix of an undirected graph is symmetric.

A(i,j) = A(j,i) for all i and j.

Page 23: Chapter 6 Graphs. 2 Outline Definitions, Terminologies and Applications Graph Representation Elementary graph operations Famous Graph Problems

23

Adjacency Matrix (Digraph)

23

1

45

1 2 3 4 512345

0 0 0 1 01 0 0 0 10 0 0 0 00 0 0 0 10 1 1 0 0

•Diagonal entries are zero.

•Adjacency matrix of a digraph need not be symmetric.

Page 24: Chapter 6 Graphs. 2 Outline Definitions, Terminologies and Applications Graph Representation Elementary graph operations Famous Graph Problems

24

Adjacency Matrix n2 bits of space For an undirected graph, may store only

lower or upper triangle (exclude diagonal). (n-1)n/2 bits

O(n) time to find vertex degree and/or vertices adjacent to a given vertex.

Page 25: Chapter 6 Graphs. 2 Outline Definitions, Terminologies and Applications Graph Representation Elementary graph operations Famous Graph Problems

25

Adjacency Lists Adjacency list for vertex i is a linear list of vertices adjacent from vertex i. An array of n adjacency lists.

23

1

45

aList[1] = (2,4)

aList[2] = (1,5)

aList[3] = (5)

aList[4] = (5,1)

aList[5] = (2,4,3)

Page 26: Chapter 6 Graphs. 2 Outline Definitions, Terminologies and Applications Graph Representation Elementary graph operations Famous Graph Problems

26

Linked Adjacency Lists Each adjacency list is a chain.

23

1

45

aList[1]

aList[5]

[2][3][4]

2 41 555 12 4 3

Array Length = n

# of chain nodes = 2e (undirected graph)

# of chain nodes = e (digraph)

Page 27: Chapter 6 Graphs. 2 Outline Definitions, Terminologies and Applications Graph Representation Elementary graph operations Famous Graph Problems

27

Weighted Graphs Cost adjacency matrix.

C(i,j) = cost of edge (i,j) Adjacency lists => each list element is a pair

(adjacent vertex, edge weight)

Page 28: Chapter 6 Graphs. 2 Outline Definitions, Terminologies and Applications Graph Representation Elementary graph operations Famous Graph Problems

28

Elementary graph operations

Visit a Graph (graph search) Connected components Spanning trees Biconnected components

Page 29: Chapter 6 Graphs. 2 Outline Definitions, Terminologies and Applications Graph Representation Elementary graph operations Famous Graph Problems

29

Graph Search Methods A vertex u is reachable from vertex v iff there

is a path from v to u.

23

8

10

1

45 9

116

7

Page 30: Chapter 6 Graphs. 2 Outline Definitions, Terminologies and Applications Graph Representation Elementary graph operations Famous Graph Problems

30

Graph Search Methods A search method starts at a given vertex v

and visits/labels/marks every vertex that is reachable from v.

23

8

10

1

45 9

116

7

Page 31: Chapter 6 Graphs. 2 Outline Definitions, Terminologies and Applications Graph Representation Elementary graph operations Famous Graph Problems

31

Graph Search Methods Many graph problems solved using a search

method. Path from one vertex to another. Is the graph connected? Find a spanning tree. Etc.

Commonly used search methods: Depth-first search. Breadth-first search.

Page 32: Chapter 6 Graphs. 2 Outline Definitions, Terminologies and Applications Graph Representation Elementary graph operations Famous Graph Problems

32

Depth-First Search Example

Start search at vertex 1.

23

8

10

1

45 9

116

7

Label vertex 1 and do a depth first search from either 2 or 4.

1

2

Suppose that vertex 2 is selected.

Page 33: Chapter 6 Graphs. 2 Outline Definitions, Terminologies and Applications Graph Representation Elementary graph operations Famous Graph Problems

33

Depth-First Search Example2

38

10

1

45 9

116

7

Label vertex 2 and do a depth first search from either 3, 5, or 6.

1

22

5

Suppose that vertex 5 is selected.

Page 34: Chapter 6 Graphs. 2 Outline Definitions, Terminologies and Applications Graph Representation Elementary graph operations Famous Graph Problems

34

Depth-First Search Example2

38

10

1

45 9

116

7

Label vertex 5 and do a depth first search from either 3, 7, or 9.

1

22

55 9

Suppose that vertex 9 is selected.

Page 35: Chapter 6 Graphs. 2 Outline Definitions, Terminologies and Applications Graph Representation Elementary graph operations Famous Graph Problems

35

Depth-First Search Example2

38

10

1

45 9

116

7

Label vertex 9 and do a depth first search from either 6 or 8.

1

22

55 99

8

Suppose that vertex 8 is selected.

Page 36: Chapter 6 Graphs. 2 Outline Definitions, Terminologies and Applications Graph Representation Elementary graph operations Famous Graph Problems

36

Depth-First Search Example2

38

10

1

45 9

116

7

Label vertex 8 and return to vertex 9.

1

22

55 99

88

From vertex 9 do a DFS(6).

6

Page 37: Chapter 6 Graphs. 2 Outline Definitions, Terminologies and Applications Graph Representation Elementary graph operations Famous Graph Problems

37

Depth-First Search Example2

38

10

1

45 9

116

7

1

22

55 99

88

Label vertex 6 and do a depth first search from either 4 or 7.

66

4

Suppose that vertex 4 is selected.

Page 38: Chapter 6 Graphs. 2 Outline Definitions, Terminologies and Applications Graph Representation Elementary graph operations Famous Graph Problems

38

Depth-First Search Example2

38

10

1

45 9

116

7

1

22

55 99

88

Label vertex 4 and return to 6.

66

44

From vertex 6 do a DFS(7).

7

Page 39: Chapter 6 Graphs. 2 Outline Definitions, Terminologies and Applications Graph Representation Elementary graph operations Famous Graph Problems

39

Depth-First Search Example2

38

10

1

45 9

116

7

1

22

55 99

88

Label vertex 7 and return to 6.

66

44

77

Return to 9.

Page 40: Chapter 6 Graphs. 2 Outline Definitions, Terminologies and Applications Graph Representation Elementary graph operations Famous Graph Problems

40

Depth-First Search Example2

38

10

1

45 9

116

7

1

22

55 99

88

66

44

77

Return to 5.

Page 41: Chapter 6 Graphs. 2 Outline Definitions, Terminologies and Applications Graph Representation Elementary graph operations Famous Graph Problems

41

Depth-First Search Example2

38

10

1

45 9

116

7

1

22

55 99

88

66

44

77

Do a DFS(3).

3

Page 42: Chapter 6 Graphs. 2 Outline Definitions, Terminologies and Applications Graph Representation Elementary graph operations Famous Graph Problems

42

Depth-First Search Example2

38

10

1

45 9

116

7

1

22

55 99

88

66

44

77

Label 3 and return to 5.

33

Return to 2.

Page 43: Chapter 6 Graphs. 2 Outline Definitions, Terminologies and Applications Graph Representation Elementary graph operations Famous Graph Problems

43

Depth-First Search Example2

38

10

1

45 9

116

7

1

22

55 99

88

66

44

77

33

Return to 1.

Page 44: Chapter 6 Graphs. 2 Outline Definitions, Terminologies and Applications Graph Representation Elementary graph operations Famous Graph Problems

44

Depth-First Search Example2

38

10

1

45 9

116

7

1

22

55 99

88

66

44

77

33

Return to invoking method.

Page 45: Chapter 6 Graphs. 2 Outline Definitions, Terminologies and Applications Graph Representation Elementary graph operations Famous Graph Problems

45

Depth-First SearchDFS(){ for (i=0 to n-1) visited[i]=0; for (v=0 to n-1) { if (visted[v]==0) DFV(v); } }

DFV(v){ visited[v]=1; //Label vertex v as visited. for (each unvisited vertex u adjacent from v) DFV(u);}

Page 46: Chapter 6 Graphs. 2 Outline Definitions, Terminologies and Applications Graph Representation Elementary graph operations Famous Graph Problems

46

Depth-First Search Property All vertices reachable from the start vertex

(including the start vertex) are visited. Time complexity

O(n2) when adjacency matrix used O(n+e) when adjacency lists used (e is number of

edges)

Page 47: Chapter 6 Graphs. 2 Outline Definitions, Terminologies and Applications Graph Representation Elementary graph operations Famous Graph Problems

47

Breadth-First Search

Visit start vertex and put into a FIFO queue. Repeatedly remove a vertex from the queue, visit

its unvisited adjacent vertices, put newly visited vertices into the queue.

Page 48: Chapter 6 Graphs. 2 Outline Definitions, Terminologies and Applications Graph Representation Elementary graph operations Famous Graph Problems

48

Breadth-First Search Example

Start search at vertex 1.

23

8

10

1

45 9

116

7

Page 49: Chapter 6 Graphs. 2 Outline Definitions, Terminologies and Applications Graph Representation Elementary graph operations Famous Graph Problems

49

Breadth-First Search Example

Visit/mark/label start vertex and put in a FIFO queue.

23

8

10

1

45 9

116

7

1

FIFO Queue

1

Page 50: Chapter 6 Graphs. 2 Outline Definitions, Terminologies and Applications Graph Representation Elementary graph operations Famous Graph Problems

50

Breadth-First Search Example

Remove 1 from Q; visit adjacent unvisited vertices;put in Q.

23

8

10

1

45 9

116

7

1

FIFO Queue

1

Page 51: Chapter 6 Graphs. 2 Outline Definitions, Terminologies and Applications Graph Representation Elementary graph operations Famous Graph Problems

51

Breadth-First Search Example

Remove 1 from Q; visit adjacent unvisited vertices;put in Q.

23

8

10

1

45 9

116

7

1

FIFO Queue2

2

4

4

Page 52: Chapter 6 Graphs. 2 Outline Definitions, Terminologies and Applications Graph Representation Elementary graph operations Famous Graph Problems

52

Breadth-First Search Example

Remove 2 from Q; visit adjacent unvisited vertices;put in Q.

23

8

10

1

45 9

116

7

1

FIFO Queue2

2

4

4

Page 53: Chapter 6 Graphs. 2 Outline Definitions, Terminologies and Applications Graph Representation Elementary graph operations Famous Graph Problems

53

Breadth-First Search Example

Remove 2 from Q; visit adjacent unvisited vertices;put in Q.

23

8

10

1

45 9

116

7

1

FIFO Queue2

4

4

5

53

3

6

6

Page 54: Chapter 6 Graphs. 2 Outline Definitions, Terminologies and Applications Graph Representation Elementary graph operations Famous Graph Problems

54

Breadth-First Search Example

Remove 4 from Q; visit adjacent unvisited vertices;put in Q.

23

8

10

1

45 9

116

7

1

FIFO Queue2

4

4

5

53

3

6

6

Page 55: Chapter 6 Graphs. 2 Outline Definitions, Terminologies and Applications Graph Representation Elementary graph operations Famous Graph Problems

55

Breadth-First Search Example

Remove 4 from Q; visit adjacent unvisited vertices;put in Q.

23

8

10

1

45 9

116

7

1

FIFO Queue2

45

53

3

6

6

Page 56: Chapter 6 Graphs. 2 Outline Definitions, Terminologies and Applications Graph Representation Elementary graph operations Famous Graph Problems

56

Breadth-First Search Example

Remove 5 from Q; visit adjacent unvisited vertices;put in Q.

23

8

10

1

45 9

116

7

1

FIFO Queue2

45

53

3

6

6

Page 57: Chapter 6 Graphs. 2 Outline Definitions, Terminologies and Applications Graph Representation Elementary graph operations Famous Graph Problems

57

Breadth-First Search Example

Remove 5 from Q; visit adjacent unvisited vertices;put in Q.

23

8

10

1

45 9

116

7

1

FIFO Queue2

45

33

6

6

9

9

7

7

Page 58: Chapter 6 Graphs. 2 Outline Definitions, Terminologies and Applications Graph Representation Elementary graph operations Famous Graph Problems

58

Breadth-First Search Example

Remove 3 from Q; visit adjacent unvisited vertices;put in Q.

23

8

10

1

45 9

116

7

1

FIFO Queue2

45

33

6

6

9

9

7

7

Page 59: Chapter 6 Graphs. 2 Outline Definitions, Terminologies and Applications Graph Representation Elementary graph operations Famous Graph Problems

59

Breadth-First Search Example

Remove 3 from Q; visit adjacent unvisited vertices;put in Q.

23

8

10

1

45 9

116

7

1

FIFO Queue2

45

3

6

6

9

9

7

7

Page 60: Chapter 6 Graphs. 2 Outline Definitions, Terminologies and Applications Graph Representation Elementary graph operations Famous Graph Problems

60

Breadth-First Search Example

Remove 6 from Q; visit adjacent unvisited vertices;put in Q.

23

8

10

1

45 9

116

7

1

FIFO Queue2

45

3

6

6

9

9

7

7

Page 61: Chapter 6 Graphs. 2 Outline Definitions, Terminologies and Applications Graph Representation Elementary graph operations Famous Graph Problems

61

Breadth-First Search Example

Remove 6 from Q; visit adjacent unvisited vertices;put in Q.

23

8

10

1

45 9

116

7

1

FIFO Queue2

45

3

6

9

9

7

7

Page 62: Chapter 6 Graphs. 2 Outline Definitions, Terminologies and Applications Graph Representation Elementary graph operations Famous Graph Problems

62

Breadth-First Search Example

Remove 9 from Q; visit adjacent unvisited vertices;put in Q.

23

8

10

1

45 9

116

7

1

FIFO Queue2

45

3

6

9

9

7

7

Page 63: Chapter 6 Graphs. 2 Outline Definitions, Terminologies and Applications Graph Representation Elementary graph operations Famous Graph Problems

63

Breadth-First Search Example

Remove 9 from Q; visit adjacent unvisited vertices;put in Q.

23

8

10

1

45 9

116

7

1

FIFO Queue2

45

3

6

9

7

78 8

Page 64: Chapter 6 Graphs. 2 Outline Definitions, Terminologies and Applications Graph Representation Elementary graph operations Famous Graph Problems

64

Breadth-First Search Example

Remove 7 from Q; visit adjacent unvisited vertices;put in Q.

23

8

10

1

45 9

116

7

1

FIFO Queue2

45

3

6

9

7

78 8

Page 65: Chapter 6 Graphs. 2 Outline Definitions, Terminologies and Applications Graph Representation Elementary graph operations Famous Graph Problems

65

Breadth-First Search Example

Remove 7 from Q; visit adjacent unvisited vertices;put in Q.

23

8

10

1

45 9

116

7

1

FIFO Queue2

45

3

6

9

7

8 8

Page 66: Chapter 6 Graphs. 2 Outline Definitions, Terminologies and Applications Graph Representation Elementary graph operations Famous Graph Problems

66

Breadth-First Search Example

Remove 8 from Q; visit adjacent unvisited vertices;put in Q.

23

8

10

1

45 9

116

7

1

FIFO Queue2

45

3

6

9

7

8 8

Page 67: Chapter 6 Graphs. 2 Outline Definitions, Terminologies and Applications Graph Representation Elementary graph operations Famous Graph Problems

67

Breadth-First Search Example

Queue is empty. Search terminates.

23

8

10

1

45 9

116

7

1

FIFO Queue2

45

3

6

9

7

8

Page 68: Chapter 6 Graphs. 2 Outline Definitions, Terminologies and Applications Graph Representation Elementary graph operations Famous Graph Problems

BFS(int v){ for (i=0 to n-1) visited[i]=0; queue_insert(v); visited[v]=1; while (queue is not empty) { v=queue_delete(); for each unvisited vertex w adjacent to v { queue_insert(w); visited[w]=1; } }}

68

Page 69: Chapter 6 Graphs. 2 Outline Definitions, Terminologies and Applications Graph Representation Elementary graph operations Famous Graph Problems

69

BFS(){ for (i=0 to n-1) visited[i]=0; while (there is any unvisted node v) { queue_insert(v); visited[v]=1; while (queue is not empty) { v=queue_delete(); for each unvisited vertex w adjacent to v { queue_insert(w); visited[w]=1; } } }}

Page 70: Chapter 6 Graphs. 2 Outline Definitions, Terminologies and Applications Graph Representation Elementary graph operations Famous Graph Problems

70

Breadth-First Search Properties

Same complexity as DFS. Time complexity

O(n2) when adjacency matrix used O(n+e) when adjacency lists used (e is number

of edges)

Page 71: Chapter 6 Graphs. 2 Outline Definitions, Terminologies and Applications Graph Representation Elementary graph operations Famous Graph Problems

71

Is The Graph Connected?

Start a depth-first search at any vertex of the graph.

Graph is connected iff all n vertices get visited.

Time O(n2) when adjacency matrix used O(n+e) when adjacency lists used (e is number

of edges)

Page 72: Chapter 6 Graphs. 2 Outline Definitions, Terminologies and Applications Graph Representation Elementary graph operations Famous Graph Problems

72

Connected Components Start a depth-first search at any as yet

unvisited vertex of the graph. Newly visited vertices (plus edges

between them) define a component. Repeat until all vertices are visited.

Page 73: Chapter 6 Graphs. 2 Outline Definitions, Terminologies and Applications Graph Representation Elementary graph operations Famous Graph Problems

73

Connected Components

23

8

10

1

45 9

116

7

Page 74: Chapter 6 Graphs. 2 Outline Definitions, Terminologies and Applications Graph Representation Elementary graph operations Famous Graph Problems

74

Connected componentDFS(){ for (i=0 to n-1) visited[i]=0; i=0; for (v=0 to n-1) { if (visted[v]==0) { output (connected component i); i++; DFV(v); } } }

DFV(v){ visired[v]=1; //Label vertex v as visited. for (each unreached vertex u adjacenct from v) DFV(u);}

Page 75: Chapter 6 Graphs. 2 Outline Definitions, Terminologies and Applications Graph Representation Elementary graph operations Famous Graph Problems

75

Time Complexity O(n2) when adjacency matrix used O(n+e) when adjacency lists used

(e is number of edges)

Page 76: Chapter 6 Graphs. 2 Outline Definitions, Terminologies and Applications Graph Representation Elementary graph operations Famous Graph Problems

76

Spanning Tree

Depth-first search from vertex 1.

Depth-first spanning tree.

23

81

45 9

67

1

22

55 99

88

66

44

77

33

Page 77: Chapter 6 Graphs. 2 Outline Definitions, Terminologies and Applications Graph Representation Elementary graph operations Famous Graph Problems

77

Spanning Tree Start a depth-first search at any vertex of the

graph. If graph is connected, the n-1 edges used to get

to unvisited vertices define a spanning tree (depth-first spanning tree).

Time O(n2) when adjacency matrix used O(n+e) when adjacency lists used (e is number of

edges) Breadth-first spanning tree

Page 78: Chapter 6 Graphs. 2 Outline Definitions, Terminologies and Applications Graph Representation Elementary graph operations Famous Graph Problems

78

Biconnected Components

Definition: A vertex v of G is an articulation point iff the deletion of v, together with the deletion of all edges incident to v, leaves behind a graph that has at least two connected components.

Definition: A biconnected graph is a connected graph that has no articulation points.

Definition: A biconnected component of a connected graph G is a maximal biconnected subgraph H of G. By maximal, we mean that G contains no other subgraph that is both biconnected and properly contains H.

Page 79: Chapter 6 Graphs. 2 Outline Definitions, Terminologies and Applications Graph Representation Elementary graph operations Famous Graph Problems

79

A Connected Graph and Its Biconnected Components

0

1

4

2 3

8

7

6

5

9

1

4

2 3

7

6

5

0

1

3 5

8

7 7

9

(a) A connected graph(b) Its biconnected components

Maximal without articulation point

Page 80: Chapter 6 Graphs. 2 Outline Definitions, Terminologies and Applications Graph Representation Elementary graph operations Famous Graph Problems

80

Biconnected Components (Cont.) Two biconnected components of the same graph

can have at most one vertex in common.

No edge can be in two or more biconnected components.

The biconnected components of G partition the edges of G.

Page 81: Chapter 6 Graphs. 2 Outline Definitions, Terminologies and Applications Graph Representation Elementary graph operations Famous Graph Problems

81

Biconnected Components (Cont.) The biconnected components of a connected,

undirected graph G can be found by using any depth-first spanning tree of G.

A nontree edge (u, v) is a back edge with respect to a spanning tree T iff either u is an ancestor of v or v is an ancestor of u.

A nontree edge that is not back edge is called a cross edge.

No graph can have cross edges with respect to any of its depth-first spanning trees.

Page 82: Chapter 6 Graphs. 2 Outline Definitions, Terminologies and Applications Graph Representation Elementary graph operations Famous Graph Problems

82

Biconnected Components (Cont.)

The root of the depth-first spanning tree is an articulation point iff it has at least two children.

Any other vertex u is an articulation point iff it has at least one child, w, such that it is not possible to reach an ancestor of u using a path composed solely of w, descendants of w, and a single back edge. ( w 必經之 vertex)

Page 83: Chapter 6 Graphs. 2 Outline Definitions, Terminologies and Applications Graph Representation Elementary graph operations Famous Graph Problems

83

Define low(w) as the lowest depth-first number that can be reached from w using a path of descendants followed by, at most, one back edge.

edge}}back a is )( | min{ }, of child a is |)(min{),(min{)(

w, xdfn(x)wxxlowwdfnwlow

Use descendent’s back edge

Use a back edge connecting with w

Page 84: Chapter 6 Graphs. 2 Outline Definitions, Terminologies and Applications Graph Representation Elementary graph operations Famous Graph Problems

84

Depth-First Spanning Tree

0

1

4

2 3

8

7

6

5

9

1

2

3

4

5

6

7

8

910

3

4

2

1

0

6

7

8

5

9

1

2

3

4

5

6

7

8

10 9

Page 85: Chapter 6 Graphs. 2 Outline Definitions, Terminologies and Applications Graph Representation Elementary graph operations Famous Graph Problems

85

dfn and low values for the Spanning Tree

vertex 0 1 2 3 4 5 6 7 8 9

dfn 5 4 3 1 2 6 7 8 10 9

low 5 1 1 1 1 6 6 6 10 9

Min{4,5,dfn(3)=1}Min{8,9,dfn(5)=6}

Min{7,min{6,10,9}}=min{7,6}: use descendent back edge

Articulation points: 1, 4, 5, 7 where it (u) has children w, low(w) >=dfn(u)

Page 86: Chapter 6 Graphs. 2 Outline Definitions, Terminologies and Applications Graph Representation Elementary graph operations Famous Graph Problems

86

u is an articulation point iff u is either the root of the spanning tree and has two or more children or u is not the root and u has a child w such that low(w) ≥ dfn(u).

implying this descendent w cannot use another way going back to the parent of u

Page 87: Chapter 6 Graphs. 2 Outline Definitions, Terminologies and Applications Graph Representation Elementary graph operations Famous Graph Problems

87

Biconnected void Biconnected()

{num = 1; //num is an int data member of graphdfn = new int[n]; //dfn is declared as int* in Graphlow = new int[n]; //low is declared as int* in Graphset entries in dfn and low to 0;Biconnected(0,-1); //start at vertex 0

}

Page 88: Chapter 6 Graphs. 2 Outline Definitions, Terminologies and Applications Graph Representation Elementary graph operations Famous Graph Problems

88

Biconnected void biconnected(const int u, const int v)

{//compute dfn and low, and output the edges of G by their biconnected //components. v is the parent(if any) of u in the resulting spanning tree. //s is an initially empty stack declared as a data member of Graph.

dfn[u] = low[u] = num++;for(each vertex w adjacent from u){//actual code uses an iterator if((v != w)&&(dfn[w] < dfn[u]) ) add(u, w) to stack s; if(dfn[w] == 0) { //w is an unvisited vertex

Biconnected(w, u);low[u] = min(low[u],low[w]);if(low[w] >= dfn[u]){ cout<<“New Biconnected Component: ” << endl; do{ delete an edge from the stack s; let this edge be (x, y); cout<<x<<“,”<<y<<endl; }while((x, y) and (u, w) are not the same edge)

} } else if(w != v) low[u] = min(low[u], dfn[w]); //back edge

}

Page 89: Chapter 6 Graphs. 2 Outline Definitions, Terminologies and Applications Graph Representation Elementary graph operations Famous Graph Problems

89

Famous Graph Problems

Bipartite graph Minimum Spanning tree Shortest path problem

Single pair shorted path All pair shorted path

Page 90: Chapter 6 Graphs. 2 Outline Definitions, Terminologies and Applications Graph Representation Elementary graph operations Famous Graph Problems

90

Bipartite graph

Two colorable

Page 91: Chapter 6 Graphs. 2 Outline Definitions, Terminologies and Applications Graph Representation Elementary graph operations Famous Graph Problems

91

Bipartite graphDFS(){ for (i=0 to n-1) visited[i]=0; for (v=0 to n-1) { if (visted[v]==0) paint(v,0); } output (bipartite graph);}

paint(v,c){ visired[v]=1; //Label vertex v as visited. color[v]=c; //Set vertex v’s color as c. for (each vertex u adjacenct from v) if (visited[u] and color[u]!=1-c) { output (not a bipartite graph!); exit(1); } else if (!visited[u]) paint(u,1-c);}

Page 92: Chapter 6 Graphs. 2 Outline Definitions, Terminologies and Applications Graph Representation Elementary graph operations Famous Graph Problems

92

Minimum-Cost Spanning Tree weighted connected undirected graph spanning tree cost of spanning tree is sum of edge costs find spanning tree that has minimum cost

Page 93: Chapter 6 Graphs. 2 Outline Definitions, Terminologies and Applications Graph Representation Elementary graph operations Famous Graph Problems

93

Example

Network has 10 edges. Spanning tree has only n - 1 = 7 edges. Need to either select 7 edges or discard 3.

1 3 5 7

2 4 6 8

2 4 6 3

8 10 14

127

9

Page 94: Chapter 6 Graphs. 2 Outline Definitions, Terminologies and Applications Graph Representation Elementary graph operations Famous Graph Problems

94

Edge Selection Strategies Start with an n-vertex 0-edge forest.

Consider edges in ascending order of cost. Select edge if it does not form a cycle together with already selected edges. Kruskal’s method.

Start with a 1-vertex tree and grow it into an n-vertex tree by repeatedly adding a vertex and an edge. When there is a choice, add a least cost edge. Prim’s method.

Page 95: Chapter 6 Graphs. 2 Outline Definitions, Terminologies and Applications Graph Representation Elementary graph operations Famous Graph Problems

95

Kruskal’s Method

Start with a forest that has no edges.

1 35 7

2 46 82 4 6 3

8 10 14

127

9

1 35 7

2 46 8

• Consider edges in ascending order of cost.• Edge (1,2) is considered first and added to the forest.

Page 96: Chapter 6 Graphs. 2 Outline Definitions, Terminologies and Applications Graph Representation Elementary graph operations Famous Graph Problems

96

Kruskal’s Method

Edge (7,8) is considered next and added.

1 35 7

2 46 82 4 6 3

8 10 14

127

9

1 35 7

2 46 82 3

• Edge (3,4) is considered next and added.

4

• Edge (5,6) is considered next and added.

6

• Edge (2,3) is considered next and added.

7

• Edge (1,3) is considered next and rejected because it creates a cycle.

Page 97: Chapter 6 Graphs. 2 Outline Definitions, Terminologies and Applications Graph Representation Elementary graph operations Famous Graph Problems

97

Kruskal’s Method

Edge (2,4) is considered next and rejected because it creates a cycle.

1 35 7

2 46 82 4 6 3

8 10 14

127

9

1 35 7

2 46 82 34

• Edge (3,5) is considered next and added.

6

10

• Edge (3,6) is considered next and rejected.

7

• Edge (5,7) is considered next and added.

14

Page 98: Chapter 6 Graphs. 2 Outline Definitions, Terminologies and Applications Graph Representation Elementary graph operations Famous Graph Problems

98

Kruskal’s Method

n - 1 edges have been selected and no cycle formed. So we must have a spanning tree. Cost is 46. Min-cost spanning tree is unique when all edge costs

are different.

1 35 7

2 46 82 4 6 3

8 10 14

127

9

1 35 7

2 46 82 34 6

10

7

14

Page 99: Chapter 6 Graphs. 2 Outline Definitions, Terminologies and Applications Graph Representation Elementary graph operations Famous Graph Problems

99

Pseudocode For Kruskal’s MethodStart with an empty set T of edges.while (E is not empty && |T| != n-1){ Let (u,v) be a least-cost edge in E. E = E - {(u,v)}. // delete edge from E if ((u,v) does not create a cycle in T) Add edge (u,v) to T.}if (| T | == n-1) T is a min-cost spanning tree.else Network has no spanning tree.

Page 100: Chapter 6 Graphs. 2 Outline Definitions, Terminologies and Applications Graph Representation Elementary graph operations Famous Graph Problems

100

Prim’s Method

Start with any single vertex tree.

1 35 7

2 46 82 4 6 3

8 10 14

127

9

5

• Get a 2-vertex tree by adding a cheapest edge.

6

6

• Get a 3-vertex tree by adding a cheapest edge.

310

• Grow the tree one edge at a time until the tree has n - 1 edges (and hence has all n vertices).

4

4

2

71

2

714

8

3

Page 101: Chapter 6 Graphs. 2 Outline Definitions, Terminologies and Applications Graph Representation Elementary graph operations Famous Graph Problems

101

Program 6.7: Prim`s algorithm //Assume that G has at least one vertex.

TV = {0}; //start with vertex 0 and no edgesfor(T = Φ; T contains fewer than n-1 edges; add(u, v) to T){

Let(u, v) be a least-cost edge such that u TV and v ∈ ∉TV;

if(there is no such edge) break;add v to TV;

}if(T contains fewer than n-1 edges) cout<<“no spanning tree”<<endl;

Page 102: Chapter 6 Graphs. 2 Outline Definitions, Terminologies and Applications Graph Representation Elementary graph operations Famous Graph Problems

102

Minimum-Cost Spanning Tree Methods

Can prove that all stated edge selection/rejection result in a minimum-cost spanning tree.

Prim’s method is fastest. O(n2) using an implementation similar to that of Dijkstra’s

shortest-path algorithm. O(e + n log n) using a Fibonacci heap.

Kruskal’s uses union-find trees to run in O(n + e log e) time.

Page 103: Chapter 6 Graphs. 2 Outline Definitions, Terminologies and Applications Graph Representation Elementary graph operations Famous Graph Problems

103

Dijkstra’s AlgorithmAssumes no negative-weight edges.Maintains a set S of vertices whose SP from s has been determined.Repeatedly selects u in V–S with minimum SP estimate (greedy choice).Store V–S in priority queue Q.

Initialize(G, s);S := ;Q := V[G];while Q do

u := Extract-Min(Q);S := S {u};for each v Adj[u] do

Relax(u, v, w)od

od

Page 104: Chapter 6 Graphs. 2 Outline Definitions, Terminologies and Applications Graph Representation Elementary graph operations Famous Graph Problems

104

Example

0

s

u v

x y

10

1

9

2

4 6

5

2 3

7

Page 105: Chapter 6 Graphs. 2 Outline Definitions, Terminologies and Applications Graph Representation Elementary graph operations Famous Graph Problems

105

Example

0

5

10

s

u v

x y

10

1

9

2

4 6

5

2 3

7

Page 106: Chapter 6 Graphs. 2 Outline Definitions, Terminologies and Applications Graph Representation Elementary graph operations Famous Graph Problems

106

Example

0

75

148

s

u v

x y

10

1

9

2

4 6

5

2 3

7

Page 107: Chapter 6 Graphs. 2 Outline Definitions, Terminologies and Applications Graph Representation Elementary graph operations Famous Graph Problems

107

Example

0

75

138

s

u v

x y

10

1

9

2

4 6

5

2 3

7

Page 108: Chapter 6 Graphs. 2 Outline Definitions, Terminologies and Applications Graph Representation Elementary graph operations Famous Graph Problems

108

Example

0

75

98

s

u v

x y

10

1

9

2

4 6

5

2 3

7

Page 109: Chapter 6 Graphs. 2 Outline Definitions, Terminologies and Applications Graph Representation Elementary graph operations Famous Graph Problems

109

Example

0

75

98

s

u v

x y

10

1

9

2

4 6

5

2 3

7