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

Post on 08-Jan-2018

223 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

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

Chapter 6Graphs

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

4

Graph

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

orientation

Proper graph No loops No multi-edges

5

Undirected Graph

23

8101

45 9 11

67

6

Directed Graph (Digraph)

23

8101

45 9 11

67

7

Examples of Graphlike Structures

0

2

1 1

2

3

0

(a) Graph with a self edge

(b) Multigraph

8

Applications—Communication Network

Vertex = city, edge = communication link.

23

8101

45 9 11

67

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

10

Street Map

Some streets are one way.

23

8101

45 9 11

67

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

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)

13

Terminologies

Undirected graph Connected

Vertices Graph

Connected component Maximal connected subgraph

degree

14

Terminologies

Directed graph Strongly connected Strongly connected component

A maximal subgraph that is strongly connected indegree outdegree

Acyclic Cycle free graph

15

Complete Undirected Graph

Has all possible edges.

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

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.

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

18

Sum Of Vertex Degrees

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

810

9 11

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

20

Graph Representation Adjacency Matrix Adjacency Lists

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

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.

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.

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.

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)

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)

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)

28

Elementary graph operations

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

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

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

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.

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.

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.

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.

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.

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

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.

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

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.

40

Depth-First Search Example2

38

10

1

45 9

116

7

1

22

55 99

88

66

44

77

Return to 5.

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

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.

43

Depth-First Search Example2

38

10

1

45 9

116

7

1

22

55 99

88

66

44

77

33

Return to 1.

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.

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);}

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)

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.

48

Breadth-First Search Example

Start search at vertex 1.

23

8

10

1

45 9

116

7

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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; } } }}

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)

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)

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.

73

Connected Components

23

8

10

1

45 9

116

7

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);}

75

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

(e is number of edges)

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

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

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.

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

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.

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.

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)

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

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

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)

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

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

}

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

}

89

Famous Graph Problems

Bipartite graph Minimum Spanning tree Shortest path problem

Single pair shorted path All pair shorted path

90

Bipartite graph

Two colorable

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);}

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

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

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.

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.

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.

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

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

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.

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

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;

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.

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

104

Example

0

s

u v

x y

10

1

9

2

4 6

5

2 3

7

105

Example

0

5

10

s

u v

x y

10

1

9

2

4 6

5

2 3

7

106

Example

0

75

148

s

u v

x y

10

1

9

2

4 6

5

2 3

7

107

Example

0

75

138

s

u v

x y

10

1

9

2

4 6

5

2 3

7

108

Example

0

75

98

s

u v

x y

10

1

9

2

4 6

5

2 3

7

109

Example

0

75

98

s

u v

x y

10

1

9

2

4 6

5

2 3

7

top related