47
Graphs 2014, Fall Pusan National University Ki-Joune Li

Graphs

Embed Size (px)

DESCRIPTION

Graphs. 2014, Fall Pusan National University Ki-Joune Li. b. c. a. a. e. d. d. c. b. e. Graph. Definition G = ( V , E ) where V : a set of vertices and E = { < u , v > | u , v  V } : a set of edges Some Properties Equivalence of Graphs Tree a Graph Minimal Graph. - PowerPoint PPT Presentation

Citation preview

Page 1: Graphs

Graphs

2014, FallPusan National UniversityKi-Joune Li

Page 2: Graphs

STEMPNU

2

Graph

Definition G = (V,E )

where V : a set of vertices and E = { <u ,v > | u ,v V } : a set of edges

Some Properties Equivalence of Graphs Tree

a Graph Minimal Graph a

b

c

d

ea

c

b

d

e

Page 3: Graphs

STEMPNU

3

Some Terms

Directed Graph G is a directed graph iff <u,v > <v,u > for u v V Otherwise G is an undirected graph

Complete Graph Suppose nv = number(V )

Undirected graph G is a complete graph if ne = nv (nv - 1) / 2, where ne is the number of edges

Adjacency For a graph G=(V,E )

u is adjacent to v iff e = <u,v > E , where u,v V If G is undirected, v is adjacent to u

otherwise v is adjacent from u

Page 4: Graphs

STEMPNU

4

Some Terms

Subgraph G’ is a subgraph of a graph G iff

V (G’ ) V (G ) and E (G’ ) E (G ) Path from u to v

A sequence of vertices u=v0, v1, v2, … vn=v V , such that<vi ,vi +1> E for every vi

Cycle iff u = v Connected

Two vertices u and v are connected iff a path from u to v

Graph G is connected iff for every pair u and v there is a path from u to v

Connected Components A connected subgraph of G

Page 5: Graphs

STEMPNU

5

Some Terms

Strongly Connected An directed graph G is strongly connected iff

iff for every pair u and v there is a path from u to v and path from v to u

DAG: directed acyclic graph (DAG)

In-degree and Out-Degree In-degree of v : number of edges coming to v Out-degree of v : number of edges going from v

Page 6: Graphs

STEMPNU

6

Representation of Graphs: Matrix

Adjacency Matrix A[i, j ] = 1, if there is an edge <vi ,vj >

A[i, j ] = 0, otherwise

Example

Undirected Graph: Symmetric Matrix Space complexity: O (nv

2 ) bits In-degree (out-degree) of a node

0

1

3

20 1 1 1

0 0 1 0

0 0 0 1

0 0 0 0

Page 7: Graphs

STEMPNU

7

Representation of Graphs: List

Adjacency List Each node has a list of adjacent nodes

Space Complexity: O (nv + ne ) Inverse Adjacent List

0

1

3

2

0 1 2 3

1 2

2 3

3

0

0

1

2

1 0

2 0

3

Page 8: Graphs

STEMPNU

8

Weighted Graph: Network

For each edge <vi ,vj >, a weight is given Example: Road Network Adjacency Matrix

A[i, j ] = wij, if there is an edge <vi ,vj > and wij is the weight A[i, j ] = , otherwise

Adjacency List

0

1

3

2

1.5

1.0

2.3 1.2

1.9

1.5 2.3 1.2

1.0

1.9

0 1

1

2

3

1.5 2 2.3 3 1.2

2 1.0

3 1.9

Page 9: Graphs

STEMPNU

9

Graph: Basic Operations

Traversal Depth First Search (DFS) Breadth First Search (BFS) Used for search Example

Find Yellow Node from 0

0

1

3

2

5

6

4

7

Page 10: Graphs

STEMPNU

10

DFS: Depth First Search

0

1

3

2

5

6

4

7

void Graph::DFS() { visited=new Boolean[n]; for(i=0;i<n;i++)visited[i]=FALSE; DFS(0); delete[] visited;}

void Graph::DFS() { visited=new Boolean[n]; for(i=0;i<n;i++)visited[i]=FALSE; DFS(0); delete[] visited;}

void Graph::DFS(int v) { visited[v]=TRUE; for each u adjacent to v { if(visited[u]==FALSE) DFS(u); }}

void Graph::DFS(int v) { visited[v]=TRUE; for each u adjacent to v { if(visited[u]==FALSE) DFS(u); }}

Adjacency List: Time Complexity: O(e)Adjacency List: Time Complexity: O(e)

Adjacency Matrix: Time Complexity: O(n2)Adjacency Matrix: Time Complexity: O(n2)

Page 11: Graphs

STEMPNU

DFS: Depth First Search

11

0

1

3

2

5

6

4

7

DFS(0)void Graph::DFS() { visited=new Boolean[n]; for(i=0;i<n;i++)visited[i]=FALSE; DFS(0); delete[] visited;}

void Graph::DFS() { visited=new Boolean[n]; for(i=0;i<n;i++)visited[i]=FALSE; DFS(0); delete[] visited;}

void Graph::DFS(int v) { visited[v]=TRUE; for each u adjacent to v { if(visited[u]==FALSE) DFS(u); }}

void Graph::DFS(int v) { visited[v]=TRUE; for each u adjacent to v { if(visited[u]==FALSE) DFS(u); }}

Page 12: Graphs

STEMPNU

DFS: Depth First Search

12

0

1

3

2

5

6

4

7

DFS(1)void Graph::DFS() { visited=new Boolean[n]; for(i=0;i<n;i++)visited[i]=FALSE; DFS(0); delete[] visited;}

void Graph::DFS() { visited=new Boolean[n]; for(i=0;i<n;i++)visited[i]=FALSE; DFS(0); delete[] visited;}

void Graph::DFS(int v) { visited[v]=TRUE; for each u adjacent to v { if(visited[u]==FALSE) DFS(u); }}

void Graph::DFS(int v) { visited[v]=TRUE; for each u adjacent to v { if(visited[u]==FALSE) DFS(u); }}

Page 13: Graphs

STEMPNU

DFS: Depth First Search

13

0

1

3

2

5

6

4

7

DFS(4)void Graph::DFS() { visited=new Boolean[n]; for(i=0;i<n;i++)visited[i]=FALSE; DFS(0); delete[] visited;}

void Graph::DFS() { visited=new Boolean[n]; for(i=0;i<n;i++)visited[i]=FALSE; DFS(0); delete[] visited;}

void Graph::DFS(int v) { visited[v]=TRUE; for each u adjacent to v { if(visited[u]==FALSE) DFS(u); }}

void Graph::DFS(int v) { visited[v]=TRUE; for each u adjacent to v { if(visited[u]==FALSE) DFS(u); }}

Page 14: Graphs

STEMPNU

DFS: Depth First Search

14

0

1

3

2

5

6

4

7

DFS(5)void Graph::DFS() { visited=new Boolean[n]; for(i=0;i<n;i++)visited[i]=FALSE; DFS(0); delete[] visited;}

void Graph::DFS() { visited=new Boolean[n]; for(i=0;i<n;i++)visited[i]=FALSE; DFS(0); delete[] visited;}

void Graph::DFS(int v) { visited[v]=TRUE; for each u adjacent to v { if(visited[u]==FALSE) DFS(u); }}

void Graph::DFS(int v) { visited[v]=TRUE; for each u adjacent to v { if(visited[u]==FALSE) DFS(u); }}

Page 15: Graphs

STEMPNU

DFS: Depth First Search

15

0

1

3

2

5

6

4

7

DFS(7)void Graph::DFS() { visited=new Boolean[n]; for(i=0;i<n;i++)visited[i]=FALSE; DFS(0); delete[] visited;}

void Graph::DFS() { visited=new Boolean[n]; for(i=0;i<n;i++)visited[i]=FALSE; DFS(0); delete[] visited;}

void Graph::DFS(int v) { visited[v]=TRUE; for each u adjacent to v { if(visited[u]==FALSE) DFS(u); }}

void Graph::DFS(int v) { visited[v]=TRUE; for each u adjacent to v { if(visited[u]==FALSE) DFS(u); }}

Page 16: Graphs

STEMPNU

16

BFS: Breadth First Search

0

1

3

2

5

6

4

7

void Graph::BFS() { visited=new Boolean[n]; for(i=0;i<n;i++)visited[i]=FALSE; Queue *queue=new Queue; queue.insert(v); while(queue.empty()!=TRUE) { v=queue.delete() for every w adjacent to v) { if(visited[w]==FALSE) { queue.insert(w); visited[w]=TRUE; } } } delete[] visited;}

void Graph::BFS() { visited=new Boolean[n]; for(i=0;i<n;i++)visited[i]=FALSE; Queue *queue=new Queue; queue.insert(v); while(queue.empty()!=TRUE) { v=queue.delete() for every w adjacent to v) { if(visited[w]==FALSE) { queue.insert(w); visited[w]=TRUE; } } } delete[] visited;}

Adjacency List: Time Complexity: O(e)Adjacency List: Time Complexity: O(e)

Adjacency Matrix: Time Complexity: O(n2)Adjacency Matrix: Time Complexity: O(n2)

Page 17: Graphs

STEMPNU

17

Spanning Tree

A subgraph T of G = (V,E ) is a spanning tree of G iff T is tree and V (T )=V (G )

Finding Spanning Tree: Traversal DFS Spanning Tree BFS Spanning Tree

0

1

3

2

5

6

4

7

0

1

3

2

5

6

4

7

0

1

3

2

5

6

4

7

Page 18: Graphs

STEMPNU

18

Articulation Point A vertex v of a graph G is an articulation point iff

the deletion of v makes G two connected components

Biconnected Graph Connected Graph without articulation point

Biconnected Components of a graph

Articulation Point and Biconnected Components

0

1

3

2

5

64

7

0

1

3

6

71

2

5

4 2 6

Page 19: Graphs

STEMPNU

19

Finding Articulation Point: DFS Tree

Back Edge and Cross Edge of DFS Spanning Tree Tree edge: edge of tree Back edge: edge to an ancestor Cross edge: neither tree edge nor back edge

No cross edge for DFS spanning tree

a

b

h

d

e

fc

g

a

b

h

d

e

f

c

gBack edge

Back edge

Page 20: Graphs

STEMPNU

20

Finding Articulation Point

Articulation point Root is an articulation point if it has at least two

children. u is an articulation point

if child of u without back edge to an ancestor of u

a

b

h

d

e

f

c

gBack edge

Back edge

Page 21: Graphs

STEMPNU

21

Finding Articulation Point by DFS Number

DFS number of a vertex: dfn (v ) The visit number by DFS

Low number: low (v ) low (v )= min{

dfn (v ),min{dfn (x )| (v, x ): back edge },

min{low (x )| x : child of v } }

v is an articulation Point If v is the root node with at least two children or If v has a child u such that low (u ) dfn (v )

v is the boss of subtree: if v dies, the subtree looses the line

Page 22: Graphs

STEMPNU

Finding Articulation Point by DFS Number

22

node e has two paths;one along dfs path, and another via back edge.

not articulation point

a

b

h

d

e

f

c

g0

0

0

2

0

5

5

5

Page 23: Graphs

STEMPNU

Finding Articulation Point by DFS Number

23

node c has two paths;one along dfs path, and another via a descendant node

not articulation point

a

b

h

d

e

f

c

g0

0

0

2

0

5

5

5

Page 24: Graphs

STEMPNU

Finding Articulation Point by DFS Number

24

node a has only one path;along dfs path

its parent node is an articulation pointsince it will be disconnected if its parent is

deleted. (node a relies only on its parent.)

a

b

h

d

e

f

c

g0

0

0

2

0

5

5

5

Page 25: Graphs

STEMPNU

25

Computation of dfs (v ) and low (v )

void Graph::DfnLow(x) { num=1; // num: class variable for(i=0;i<n;i++) dfn[i]=low[i]=0; DfnLow(x,-1);//x is the root node}

void Graph::DfnLow(x) { num=1; // num: class variable for(i=0;i<n;i++) dfn[i]=low[i]=0; DfnLow(x,-1);//x is the root node}

void Graph::DfnLow(int u,v) { dfn[u]=low[u]=num++; for(each vertex w adjacent from u){ if(dfn[w]==0) // unvisited w DfnLow(w,u); low[w]=min(low[u],low[w]); else if(w!=v) low[u]=min(low[u],dfn[w]); //back edge }}

void Graph::DfnLow(int u,v) { dfn[u]=low[u]=num++; for(each vertex w adjacent from u){ if(dfn[w]==0) // unvisited w DfnLow(w,u); low[w]=min(low[u],low[w]); else if(w!=v) low[u]=min(low[u],dfn[w]); //back edge }}

Adjacency List: Time Complexity: O(e)Adjacency List: Time Complexity: O(e)

Adjacency Matrix: Time Complexity: O(n2)Adjacency Matrix: Time Complexity: O(n2)

Page 26: Graphs

STEMPNU

26

Minimum Spanning Tree

G is a weighted graph T is the MST of G iff T is a spanning tree of G and

for any other spanning tree of G, C (T ) < C (T’ )

0

1

3

2

5

64

7

7

5

10

2

4

6

38

12

15

0

1

3

2

5

64

75

2

4

6

38

12

Page 27: Graphs

STEMPNU

27

Finding MST

Greedy Algorithm Choosing a next branch which looks like better than

others. Not always the optimal solution

Kruskal’s Algorithm and Prim’s Algorithm Two greedy algorithms to find MST

Current state

Solution by greedy algorithm,only locally optimal

Globally optimal solution

Page 28: Graphs

STEMPNU

28

Kruskal’s Algorithm

Algorithm KruskalMST Input: Graph G Output: MST T Begin T {}; while( n(T)<n-1 and G.E is not empty) { (v,w) smallest edge of G.E; G.E G.E-{(v,w)}; if no cycle in {(v,w)}T, T {(v,w)}T } if(n(T)<n-1) cout<<“No MST”;End Algorithm

Algorithm KruskalMST Input: Graph G Output: MST T Begin T {}; while( n(T)<n-1 and G.E is not empty) { (v,w) smallest edge of G.E; G.E G.E-{(v,w)}; if no cycle in {(v,w)}T, T {(v,w)}T } if(n(T)<n-1) cout<<“No MST”;End Algorithm

0

1

3

2

5

64

7

7

510

2

4

6

38

12

15

X

XT is MSTT is MST

Time Complexity: O(e loge)Time Complexity: O(e loge)

Page 29: Graphs

STEMPNU

29

Checking Cycles for Kruskal’s Algorithm

v

1

2

V

3

4

5

8

6

V1 V2

If v V and w V ,

then cycle,

otherwise no cycle

w

log n

Page 30: Graphs

STEMPNU

30

Prim’s Algorithm

Algorithm PrimMST Input: Graph G Output: MST T Begin Vnew {v0}; Enew {}; while( n(Enew)<n-1) { select v such that (u,v) is the smallest edge where u Vnew, v Vnew; if no such v, break; G.E G.E-{(u,v)}; Enew {(u,v)} Enew; Vnew {v} Vnew; } if(n(T)<n-1) cout<<“No MST”;End Algorithm

Algorithm PrimMST Input: Graph G Output: MST T Begin Vnew {v0}; Enew {}; while( n(Enew)<n-1) { select v such that (u,v) is the smallest edge where u Vnew, v Vnew; if no such v, break; G.E G.E-{(u,v)}; Enew {(u,v)} Enew; Vnew {v} Vnew; } if(n(T)<n-1) cout<<“No MST”;End Algorithm

0

1

3

2

5

64

7

7

510

2

4

6

38

12

15

T is the MSTT is the MST

Time Complexity: O( n 2)Time Complexity: O( n 2)

Page 31: Graphs

STEMPNU

31

Finding the Edge with Min-Cost

0

13

2

Step 1

n

01

32

n - 1

Step 2

n + (n-1) +… 1 = O( n2 )

TVV

TVT

Page 32: Graphs

STEMPNU

32

Shortest Path Problem

Shortest Path From 0 to all other

verticesvertex cost

1 5

2 6

3 10

4 8

5 7

6 13

7 11

0

1

3

2

5

6

4

7

5

8 10

615

13

12

11

9

3

2

4 6

1

Page 33: Graphs

STEMPNU

33

Shortest Path Problem

Shortest Path Step 1

vertex cost

1 5

2 8

3 10

4 ?

5 ?

6 ?

7 ?

0

1

3

2

5

6

4

7

5

8 10

615

13

12

11

9

3

2

4 6

1

TV

V-TV

Page 34: Graphs

STEMPNU

34

Shortest Path Problem

Shortest Path Step 1

vertex cost

1 5

2 8

3 10

4 ?

5 ?

6 ?

7 ?

0

1

3

2

5

6

4

7

5

8 10

615

13

12

11

9

3

2

4 6

1

TV

V-TV

Page 35: Graphs

STEMPNU

35

Shortest Path Problem

Shortest Path Step 2

vertex cost

1 5

2 86

3 10

4 ?8

5 ?7

6 ?

7 ?

0

1

3

2

5

6

4

7

5

8 10

615

13

12

11

9

3

2

4 6

1

5 + 1 ? 8

5 + 3 ? ∞

TV

V-TV

5 + 2 ? ∞

Page 36: Graphs

STEMPNU

36

Shortest Path Problem

Shortest Path Step 2

vertex cost

1 5

2 86

3 10

4 ?8

5 ?7

6 ?

7 ?

0

1

3

2

5

6

4

7

5

8 10

615

13

12

11

9

3

2

4 6

1

5 + 1 ? 8

5 + 3 ? ∞

TV

V-TV

5 + 2 ? ∞

Page 37: Graphs

STEMPNU

37

Shortest Path Problem

Shortest Path Step 2

vertex cost

1 5

2 86

3 10

4 ?8

5 ?7

6 ?

7 ?

0

1

3

2

5

6

4

7

5

8 10

615

13

12

11

9

3

2

4 6

1

6 + 15 ? ∞

TV

V-TV

6 + 6 ? ∞

Page 38: Graphs

STEMPNU

38

Finding Shortest Path from Single Source (Nonnegative Weight)

Algorithm DijkstraShortestPath(G) /* G=(V,E) */ output: Shortest Path Length Array D[n] Begin S {v0}; D[v0]0; for each v in V-{v0}, do D[v] c(v0,v); while S V do begin choose a vertex w in V-S such that D[w] is minimum; add w to S; for each v in V-S do D[v] min(D[v],D[w]+c(w,v)); endEnd Algorithm

Algorithm DijkstraShortestPath(G) /* G=(V,E) */ output: Shortest Path Length Array D[n] Begin S {v0}; D[v0]0; for each v in V-{v0}, do D[v] c(v0,v); while S V do begin choose a vertex w in V-S such that D[w] is minimum; add w to S; for each v in V-S do D[v] min(D[v],D[w]+c(w,v)); endEnd Algorithm

0 1

2

3

4

2

10 37

5 4

6

w

uv

SO( n2 )

Page 39: Graphs

STEMPNU

39

Finding Shortest Path from Single Source (Nonnegative Weight)

0 1

2

3

4

2

10∞

1: {v0}

0 1

2

3

4

2

95

2: {v0, v1}

0 1

2

3

4

2

95

9

3: {v0, v1, v2}

9

0 1

2

3

4

2

5

9

4: {v0, v1, v2, v4}

0 1

234

2

95

9

5: {v0, v1, v2, v3, v4}

0 1

2

3

4

2

10 37

5 4

6

Page 40: Graphs

STEMPNU

40

Transitive Closure

0 1

2

3

4

00100

10000

01000

00100

00010

A

11100

11100

11100

11100

11110

A+ A*

0 1

2

3

4

11100

11100

11100

11110

11111

0 1

2

3

4

Transitive Closure Set of reachable nodes

Page 41: Graphs

STEMPNU

41

Transitive Closure

Void Graph::TransitiveClosure { for(int i=0;i<n;i++) for (int j=0;i<n;j++) a[i][j]=c[i][j]; for(int k=0;k<n;k++) for(int i=0;i<n;i++) for (int j=0;i<n;j++) a[i][j]= a[i][j]||(a[i][k] && a[k][j]);}

Void Graph::TransitiveClosure { for(int i=0;i<n;i++) for (int j=0;i<n;j++) a[i][j]=c[i][j]; for(int k=0;k<n;k++) for(int i=0;i<n;i++) for (int j=0;i<n;j++) a[i][j]= a[i][j]||(a[i][k] && a[k][j]);}

O ( n3 )

Void Graph::AllPairsShortestPath { for(int i=0;i<n;i++) for (int j=0;i<n;j++) a[i][j]=c[i][j]; for(int k=0;k<n;k++) for(int i=0;i<n;i++) for (int j=0;i<n;j++) a[i][j]= min(a[i][j],(a[i][k]+a[k][j]));}

Void Graph::AllPairsShortestPath { for(int i=0;i<n;i++) for (int j=0;i<n;j++) a[i][j]=c[i][j]; for(int k=0;k<n;k++) for(int i=0;i<n;i++) for (int j=0;i<n;j++) a[i][j]= min(a[i][j],(a[i][k]+a[k][j]));}

O ( n3 )

Page 42: Graphs

STEMPNU

Activity Networks

42

Page 43: Graphs

STEMPNU

AOV – Activity-on-vertex

43

0

1

3

2

5

6

4

7

Node 1 is a immediate successor of Node 0Node 1 is a immediate successor of Node 0

Node 5 is a immediate successor of Node 1Node 5 is a immediate successor of Node 1

Node 5 is a successor of Node 0Node 5 is a successor of Node 0

Node 0 is a predecessor of Node 5Node 0 is a predecessor of Node 5

Partial order: for some pairs (v, w) (v, w V ), v w (but not for all pairs)(cf. Total Order: for every pair (v, w) (v, w V ), v w or w v )Partial order: for some pairs (v, w) (v, w V ), v w (but not for all pairs)(cf. Total Order: for every pair (v, w) (v, w V ), v w or w v )

Node 0 Node 1Node 0 Node 1

Node 1 Node 5Node 1 Node 5

Node 0 Node 5Node 0 Node 5

No Cycle No Cycle Transitive and Irreflexive Transitive and Irreflexive

Page 44: Graphs

STEMPNU

Topological Order

44

0

1

3

2

5

6

4

7

Ordering: (0, 1, 2, 3, 4, 5, 7, 6)

Ordering: (0, 1, 4, 2, 3, 5, 7, 6)

Ordering: (0, 1, 2, 3, 4, 5, 7, 6)

Ordering: (0, 1, 4, 2, 3, 5, 7, 6)

Both orderings satisfy the partial orderBoth orderings satisfy the partial order

Topological order: Ordering by partial orderTopological order: Ordering by partial order

Ordering: (0, 1, 2, 5, 4, 3, 7, 6): Not a topological orderOrdering: (0, 1, 2, 5, 4, 3, 7, 6): Not a topological order

Page 45: Graphs

STEMPNU

Topological Ordering

45

0

1

3

2

5

6

4

7

1

3

2

5

6

4

7

3

2

5

6

4

7

00

0, 10, 1

3

2

5

67

0, 1, 40, 1, 4

35

67

0, 1, 4, 30, 1, 4, 3

Page 46: Graphs

STEMPNU

Topological Ordering

46

Void Topological_Ordering_AOV(Digraph G) for each node v in G.V { if v has no predecessor { cout << v; delete v from G.V; deleve (v,w) from G.E; } } if G.V is not empty, cout <<“Cycle found\n”;}

Void Topological_Ordering_AOV(Digraph G) for each node v in G.V { if v has no predecessor { cout << v; delete v from G.V; deleve (v,w) from G.E; } } if G.V is not empty, cout <<“Cycle found\n”;}

O ( n + e )

Page 47: Graphs

STEMPNU

AOE – Activity-on-edgePERT (Project Evaluation and Review Technique

47

0

1

2

5

6

4

7

5

8

6

12

11

9

3

2

46

1

Node 2 is completed only if every predecessor is completed

Earliest Time: the LONGEST PATH from node 0 Earliest time of node 1: 5 Earliest time of node 2: 8 Earliest time of node 4: 8 Earliest time of node 5: max(8+11, 5+2, 8+6)=19 Earliest time of node 7: max(8+9, 19+4)=23 Earliest time of node 6: max(23+12, 19+6)=35

Node 2 is completed only if every predecessor is completed

Earliest Time: the LONGEST PATH from node 0 Earliest time of node 1: 5 Earliest time of node 2: 8 Earliest time of node 4: 8 Earliest time of node 5: max(8+11, 5+2, 8+6)=19 Earliest time of node 7: max(8+9, 19+4)=23 Earliest time of node 6: max(23+12, 19+6)=35

(0, 1, 4, 5, 7, 6) : Critical Path

By reducing the length on the critical path,we can reduce the length of total path.

(0, 1, 4, 5, 7, 6) : Critical Path

By reducing the length on the critical path,we can reduce the length of total path.

0

1

2

5

6

4

7

5

8

6

12

11

9

3

2

46

1