intro graph algorithms

117
Graphs Unweighted Graphs Bjarki Ágúst Guðmundsson Tómas Ken Magnússon School of Computer Science Reykja vík University Árangursrík forritun og lausn verkefna

Upload: thahir-shah

Post on 10-Jan-2016

242 views

Category:

Documents


0 download

DESCRIPTION

Graph Algorithms

TRANSCRIPT

Page 1: Intro Graph Algorithms

7/18/2019 Intro Graph Algorithms

http://slidepdf.com/reader/full/intro-graph-algorithms 1/117

GraphsUnweighted Graphs

Bjarki Ágúst GuðmundssonTómas Ken Magnússon

School of Computer Science

Reykjavík University

Árangursrík forritun og lausn verkefna

Page 2: Intro Graph Algorithms

7/18/2019 Intro Graph Algorithms

http://slidepdf.com/reader/full/intro-graph-algorithms 2/117

Today we’re going to cover

▶  Graph basics▶  Graph representation (recap)▶  Depth-rst search▶  Connected components

▶  DFS tree▶   Bridges▶  Strongly connected components▶  Topological sort▶  Breadth-rst search▶  Shortest paths in unweighted graphs

2

Page 3: Intro Graph Algorithms

7/18/2019 Intro Graph Algorithms

http://slidepdf.com/reader/full/intro-graph-algorithms 3/117

What is a graph?

3

Page 4: Intro Graph Algorithms

7/18/2019 Intro Graph Algorithms

http://slidepdf.com/reader/full/intro-graph-algorithms 4/117

What is a graph?

▶   Vertices

– Road intersections– Computers– Floors in a house– Objects

0

1 2

3

3

Page 5: Intro Graph Algorithms

7/18/2019 Intro Graph Algorithms

http://slidepdf.com/reader/full/intro-graph-algorithms 5/117

What is a graph?

▶   Vertices

– Road intersections– Computers– Floors in a house– Objects

▶   Edges– Roads– Ethernet cables– Stairs or elevators– Relation between objects

0

1 2

3

3

Page 6: Intro Graph Algorithms

7/18/2019 Intro Graph Algorithms

http://slidepdf.com/reader/full/intro-graph-algorithms 6/117

Types of edges

▶   Unweighted

0

1 2

3

4

Page 7: Intro Graph Algorithms

7/18/2019 Intro Graph Algorithms

http://slidepdf.com/reader/full/intro-graph-algorithms 7/117

Types of edges

▶  Unweighted or Weighted

0

1 2

3

3   -4

0

2.9

4

Page 8: Intro Graph Algorithms

7/18/2019 Intro Graph Algorithms

http://slidepdf.com/reader/full/intro-graph-algorithms 8/117

Types of edges

▶  Unweighted or Weighted▶   Undirected   0

1 2

3

4

Page 9: Intro Graph Algorithms

7/18/2019 Intro Graph Algorithms

http://slidepdf.com/reader/full/intro-graph-algorithms 9/117

Types of edges

▶  Unweighted or Weighted▶  Undirected or Directed   0

1 2

3

4

Page 10: Intro Graph Algorithms

7/18/2019 Intro Graph Algorithms

http://slidepdf.com/reader/full/intro-graph-algorithms 10/117

Types of edges

▶  Unweighted or Weighted▶  Undirected or Directed   0

1 2

3

4

Page 11: Intro Graph Algorithms

7/18/2019 Intro Graph Algorithms

http://slidepdf.com/reader/full/intro-graph-algorithms 11/117

Multigraphs

0

1 2

3

5

Page 12: Intro Graph Algorithms

7/18/2019 Intro Graph Algorithms

http://slidepdf.com/reader/full/intro-graph-algorithms 12/117

Multigraphs

▶  Multiple edges

0

1 2

3

5

Page 13: Intro Graph Algorithms

7/18/2019 Intro Graph Algorithms

http://slidepdf.com/reader/full/intro-graph-algorithms 13/117

Multigraphs

▶  Multiple edges▶   Self-loops   0

1 2

3

5

Page 14: Intro Graph Algorithms

7/18/2019 Intro Graph Algorithms

http://slidepdf.com/reader/full/intro-graph-algorithms 14/117

Adjacency list

0:   1,   2

1:   0,   22:   0,   1,   3

3:   2

vector<int>   adj[4];adj[0].push_back(1);

adj[0].push_back(2);

adj[1].push_back(0);

adj[1].push_back(2);

adj[2].push_back(0);

adj[2].push_back(1);

adj[2].push_back(3);

adj[3].push_back(2);

0

1 2

3

6

Page 15: Intro Graph Algorithms

7/18/2019 Intro Graph Algorithms

http://slidepdf.com/reader/full/intro-graph-algorithms 15/117

Adjacency list (directed)

0:   1

1:   22:   0,   1,   3

3:

vector<int>   adj[4];adj[0].push_back(1);

adj[1].push_back(2);

adj[2].push_back(0);

adj[2].push_back(1);

adj[2].push_back(3);

0

1 2

3

7

Page 16: Intro Graph Algorithms

7/18/2019 Intro Graph Algorithms

http://slidepdf.com/reader/full/intro-graph-algorithms 16/117

Vertex properties (undirected graph)

▶  Degree of a vertex– Number of adjacent edges– Number of adjacent vertices

0

1 2

3

8

Page 17: Intro Graph Algorithms

7/18/2019 Intro Graph Algorithms

http://slidepdf.com/reader/full/intro-graph-algorithms 17/117

Vertex properties (undirected graph)

▶  Degree of a vertex– Number of adjacent edges– Number of adjacent vertices

0

1 2

3

2

2   3

1

8

Page 18: Intro Graph Algorithms

7/18/2019 Intro Graph Algorithms

http://slidepdf.com/reader/full/intro-graph-algorithms 18/117

Vertex properties (undirected graph)

▶  Degree of a vertex– Number of adjacent edges– Number of adjacent vertices

▶  Handshaking lemma∑

v ∈V 

deg(v ) = 2|V |

0

1 2

3

2

2   3

1

8

Page 19: Intro Graph Algorithms

7/18/2019 Intro Graph Algorithms

http://slidepdf.com/reader/full/intro-graph-algorithms 19/117

Vertex properties (undirected graph)

▶  Degree of a vertex– Number of adjacent edges– Number of adjacent vertices

▶  Handshaking lemma∑

v ∈V 

deg(v ) = 2|V |

2 + 2 + 3 + 1 = 2 × 4

0

1 2

3

2

2   3

1

8

V i ( di d h)

Page 20: Intro Graph Algorithms

7/18/2019 Intro Graph Algorithms

http://slidepdf.com/reader/full/intro-graph-algorithms 20/117

Vertex properties (undirected graph)

0:   1,   2

1:   0,   22:   0,   1,   3

3:   2

adj[0].size()   // 2adj[1].size()   // 2

adj[2].size()   // 3

adj[3].size()   // 1

0

1 2

3

2

2   3

1

9

V t ti (di t d h)

Page 21: Intro Graph Algorithms

7/18/2019 Intro Graph Algorithms

http://slidepdf.com/reader/full/intro-graph-algorithms 21/117

Vertex properties (directed graph)

▶  Outdegree of a vertex

– Number of outgoing edges   0

1 2

3

10

V t ti (di t d h)

Page 22: Intro Graph Algorithms

7/18/2019 Intro Graph Algorithms

http://slidepdf.com/reader/full/intro-graph-algorithms 22/117

Vertex properties (directed graph)

▶  Outdegree of a vertex

– Number of outgoing edges   0

1 2

3

1

1   3

0

10

V t ti (di t d h)

Page 23: Intro Graph Algorithms

7/18/2019 Intro Graph Algorithms

http://slidepdf.com/reader/full/intro-graph-algorithms 23/117

Vertex properties (directed graph)

▶  Outdegree of a vertex

– Number of outgoing edges▶  Indegree of a vertex

– Number of incoming edges

0

1 2

3

10

Verte properties (directed graph)

Page 24: Intro Graph Algorithms

7/18/2019 Intro Graph Algorithms

http://slidepdf.com/reader/full/intro-graph-algorithms 24/117

Vertex properties (directed graph)

▶  Outdegree of a vertex

– Number of outgoing edges▶  Indegree of a vertex

– Number of incoming edges

0

1 2

3

1

2 1

1

10

Vertex properties (directed graph)

Page 25: Intro Graph Algorithms

7/18/2019 Intro Graph Algorithms

http://slidepdf.com/reader/full/intro-graph-algorithms 25/117

Vertex properties (directed graph)

▶  Outdegree of a vertex

– Number of outgoing edges▶  Indegree of a vertex

– Number of incoming edges

0

1 2

3

10

Vertex properties (directed graph)

Page 26: Intro Graph Algorithms

7/18/2019 Intro Graph Algorithms

http://slidepdf.com/reader/full/intro-graph-algorithms 26/117

Vertex properties (directed graph)

▶  Outdegree of a vertex

– Number of outgoing edges▶  Indegree of a vertex

– Number of incoming edges

0

1 2

3

1

1   3

0

10

Adjacency list (directed)

Page 27: Intro Graph Algorithms

7/18/2019 Intro Graph Algorithms

http://slidepdf.com/reader/full/intro-graph-algorithms 27/117

Adjacency list (directed)

0:   1

1:   22:   0,   1,   3

3:

adj[0].size()   // 1adj[1].size()   // 1

adj[2].size()   // 3

adj[3].size()   // 0

0

1 2

3

1

1   3

0

11

Paths

Page 28: Intro Graph Algorithms

7/18/2019 Intro Graph Algorithms

http://slidepdf.com/reader/full/intro-graph-algorithms 28/117

Paths

▶  Path / Walk / Trail:

e1e2   . . . ek 

such that

ei  ∈ E 

ei  = e j  ⇒ i  =  j 

to(ei ) = from(ei +1)

0

1 2

3

12

Paths

Page 29: Intro Graph Algorithms

7/18/2019 Intro Graph Algorithms

http://slidepdf.com/reader/full/intro-graph-algorithms 29/117

Paths

▶  Path / Walk / Trail:

e1e2   . . . ek 

such that

ei  ∈ E 

ei  = e j  ⇒ i  =  j 

to(ei ) = from(ei +1)

0

1 2

3

e1

e3

e2

12

Paths

Page 30: Intro Graph Algorithms

7/18/2019 Intro Graph Algorithms

http://slidepdf.com/reader/full/intro-graph-algorithms 30/117

Paths

▶  Path / Walk / Trail:

e1e2   . . . ek 

such that

ei  ∈ E 

ei  = e j  ⇒ i  =  j 

to(ei ) = from(ei +1)

0

1 2

3

e1

12

Paths

Page 31: Intro Graph Algorithms

7/18/2019 Intro Graph Algorithms

http://slidepdf.com/reader/full/intro-graph-algorithms 31/117

Paths

▶  Path / Walk / Trail:

e1e2   . . . ek 

such that

ei  ∈ E 

ei  = e j  ⇒ i  =  j 

to(ei ) = from(ei +1)

0

1 2

3

e3   e2

e1

e4

12

Cycles

Page 32: Intro Graph Algorithms

7/18/2019 Intro Graph Algorithms

http://slidepdf.com/reader/full/intro-graph-algorithms 32/117

Cycles

▶  Cycle / Circuit / Tour:

e1e2   . . . ek 

such that

ei  ∈ E 

ei  = e j  ⇒ i  =  j 

to(ei ) = from(ei +1)

from(e1) = to(ek )

0

1 2

3

13

Cycles

Page 33: Intro Graph Algorithms

7/18/2019 Intro Graph Algorithms

http://slidepdf.com/reader/full/intro-graph-algorithms 33/117

Cycles

▶  Cycle / Circuit / Tour:

e1e2   . . . ek 

such that

ei  ∈ E 

ei  = e j  ⇒ i  =  j 

to(ei ) = from(ei +1)

from(e1) = to(ek )

0

1 2

3

e1e3

e2

13

Cycles

Page 34: Intro Graph Algorithms

7/18/2019 Intro Graph Algorithms

http://slidepdf.com/reader/full/intro-graph-algorithms 34/117

Cycles

▶  Cycle / Circuit / Tour:

e1e2   . . . ek 

such that

ei  ∈ E 

ei  = e j  ⇒ i  =  j 

to(ei ) = from(ei +1)

from(e1) = to(ek )

0

1 2

3

e2e1

e3

13

Cycles

Page 35: Intro Graph Algorithms

7/18/2019 Intro Graph Algorithms

http://slidepdf.com/reader/full/intro-graph-algorithms 35/117

Cycles

▶  Cycle / Circuit / Tour:

e1e2   . . . ek 

such that

ei  ∈ E 

ei  = e j  ⇒ i  =  j 

to(ei ) = from(ei +1)

from(e1) = to(ek )

0

1 2

3

e3e1

e2

13

Depth-rst search

Page 36: Intro Graph Algorithms

7/18/2019 Intro Graph Algorithms

http://slidepdf.com/reader/full/intro-graph-algorithms 36/117

Depth rst search

▶  Given a graph (either directed or undirected) and two

vertices u and  v , does there exist a path from  u to v ?▶  Depth-rst search is an algorithm for nding such a

path, if one exists

▶  It traverses the graph in depth-rst order, starting

from the initial vertex u▶  We don’t actually have to specify a v , since we can

 just let it visit all reachable vertices from u (and stillsame time complexity)

▶   But what is the time complexity?▶   Each vertex is visited once, and each edge is

traversed once▶   O(n + m)

14

Depth-rst search

Page 37: Intro Graph Algorithms

7/18/2019 Intro Graph Algorithms

http://slidepdf.com/reader/full/intro-graph-algorithms 37/117

Depth rst search

0

1

2

3

4

5   6

7

8

9

10

Stack: |

0 1 2 3 4 5 6 7 8 9 10

 marked 0 0 0 0 0 0 0 0 0 0 0

15

Depth-rst search

Page 38: Intro Graph Algorithms

7/18/2019 Intro Graph Algorithms

http://slidepdf.com/reader/full/intro-graph-algorithms 38/117

p

0

1

2

3

4

5   6

7

8

9

10

0

Stack:   0   |

0 1 2 3 4 5 6 7 8 9 10

 marked   1   0 0 0 0 0 0 0 0 0 0

15

Depth-rst search

Page 39: Intro Graph Algorithms

7/18/2019 Intro Graph Algorithms

http://slidepdf.com/reader/full/intro-graph-algorithms 39/117

p

0

1

2

3

4

5   6

7

8

9

10

0

Stack:   0   |

0 1 2 3 4 5 6 7 8 9 10

 marked   1   0 0 0 0 0 0 0 0 0 0

15

Depth-rst search

Page 40: Intro Graph Algorithms

7/18/2019 Intro Graph Algorithms

http://slidepdf.com/reader/full/intro-graph-algorithms 40/117

p

0

1

2

3

4

5   6

7

8

9

10

0

Stack:   0   | 2 1

0 1 2 3 4 5 6 7 8 9 10

 marked   1 1 1   0 0 0 0 0 0 0 0

15

Depth-rst search

Page 41: Intro Graph Algorithms

7/18/2019 Intro Graph Algorithms

http://slidepdf.com/reader/full/intro-graph-algorithms 41/117

p

0

1

2

3

4

5   6

7

8

9

10

0

2

Stack:   2   | 1

0 1 2 3 4 5 6 7 8 9 10

 marked   1 1 1   0 0 0 0 0 0 0 0

15

Depth-rst search

Page 42: Intro Graph Algorithms

7/18/2019 Intro Graph Algorithms

http://slidepdf.com/reader/full/intro-graph-algorithms 42/117

p

0

1

2

3

4

5   6

7

8

9

10

0

2

Stack:   2   | 1

0 1 2 3 4 5 6 7 8 9 10

 marked   1 1 1   0 0 0 0 0 0 0 0

15

Depth-rst search

Page 43: Intro Graph Algorithms

7/18/2019 Intro Graph Algorithms

http://slidepdf.com/reader/full/intro-graph-algorithms 43/117

0

1

2

3

4

5   6

7

8

9

10

0

2

Stack:   2   | 3 1

0 1 2 3 4 5 6 7 8 9 10

 marked   1 1 1 1   0 0 0 0 0 0 0

15

Depth-rst search

Page 44: Intro Graph Algorithms

7/18/2019 Intro Graph Algorithms

http://slidepdf.com/reader/full/intro-graph-algorithms 44/117

0

1

2

3

4

5   6

7

8

9

10

0

2

3

Stack:   3   | 1

0 1 2 3 4 5 6 7 8 9 10

 marked   1 1 1 1   0 0 0 0 0 0 0

15

Depth-rst search

Page 45: Intro Graph Algorithms

7/18/2019 Intro Graph Algorithms

http://slidepdf.com/reader/full/intro-graph-algorithms 45/117

0

1

2

3

4

5   6

7

8

9

10

0

2

3

1

Stack:   1   |

0 1 2 3 4 5 6 7 8 9 10

 marked   1 1 1 1   0 0 0 0 0 0 0

15

Depth-rst search

Page 46: Intro Graph Algorithms

7/18/2019 Intro Graph Algorithms

http://slidepdf.com/reader/full/intro-graph-algorithms 46/117

0

1

2

3

4

5   6

7

8

9

10

0

2

3

1

Stack:   1   |

0 1 2 3 4 5 6 7 8 9 10

 marked   1 1 1 1   0 0 0 0 0 0 0

15

Depth-rst search

Page 47: Intro Graph Algorithms

7/18/2019 Intro Graph Algorithms

http://slidepdf.com/reader/full/intro-graph-algorithms 47/117

0

1

2

3

4

5   6

7

8

9

10

0

2

3

1

Stack:   1   | 4

0 1 2 3 4 5 6 7 8 9 10

 marked   1 1 1 1 1   0 0 0 0 0 0

15

Depth-rst search

Page 48: Intro Graph Algorithms

7/18/2019 Intro Graph Algorithms

http://slidepdf.com/reader/full/intro-graph-algorithms 48/117

0

1

2

3

4

5   6

7

8

9

10

0

2

3

14

Stack:   4   |

0 1 2 3 4 5 6 7 8 9 10

 marked   1 1 1 1 1   0 0 0 0 0 0

15

Depth-rst search

Page 49: Intro Graph Algorithms

7/18/2019 Intro Graph Algorithms

http://slidepdf.com/reader/full/intro-graph-algorithms 49/117

0

1

2

3

4

5   6

7

8

9

10

0

2

3

14

Stack:   4   |

0 1 2 3 4 5 6 7 8 9 10

 marked   1 1 1 1 1   0 0 0 0 0 0

15

Depth-rst search

Page 50: Intro Graph Algorithms

7/18/2019 Intro Graph Algorithms

http://slidepdf.com/reader/full/intro-graph-algorithms 50/117

0

1

2

3

4

5   6

7

8

9

10

0

2

3

14

Stack:   4   | 5

0 1 2 3 4 5 6 7 8 9 10

 marked   1 1 1 1 1 1   0 0 0 0 0

15

Depth-rst search

Page 51: Intro Graph Algorithms

7/18/2019 Intro Graph Algorithms

http://slidepdf.com/reader/full/intro-graph-algorithms 51/117

0

1

2

3

4

5   6

7

8

9

10

0

2

3

14

5

Stack:   5   |

0 1 2 3 4 5 6 7 8 9 10

 marked   1 1 1 1 1 1   0 0 0 0 0

15

Depth-rst search

Page 52: Intro Graph Algorithms

7/18/2019 Intro Graph Algorithms

http://slidepdf.com/reader/full/intro-graph-algorithms 52/117

0

1

2

3

4

5   6

7

8

9

10

0

2

3

14

5

Stack:   5   |

0 1 2 3 4 5 6 7 8 9 10

 marked   1 1 1 1 1 1   0 0 0 0 0

15

Depth-rst search

Page 53: Intro Graph Algorithms

7/18/2019 Intro Graph Algorithms

http://slidepdf.com/reader/full/intro-graph-algorithms 53/117

0

1

2

3

4

5   6

7

8

9

10

0

2

3

14

5

Stack:   5   | 8 6 7

0 1 2 3 4 5 6 7 8 9 10

 marked   1 1 1 1 1 1 1 1 1   0 0

15

Depth-rst search

Page 54: Intro Graph Algorithms

7/18/2019 Intro Graph Algorithms

http://slidepdf.com/reader/full/intro-graph-algorithms 54/117

0

1

2

3

4

5   6

7

8

9

10

0

2

3

14

5

8

Stack:   8   | 6 7

0 1 2 3 4 5 6 7 8 9 10

 marked   1 1 1 1 1 1 1 1 1   0 0

15

Depth-rst search

Page 55: Intro Graph Algorithms

7/18/2019 Intro Graph Algorithms

http://slidepdf.com/reader/full/intro-graph-algorithms 55/117

0

1

2

3

4

5   6

7

8

9

10

0

2

3

14

5

8

Stack:   8   | 6 7

0 1 2 3 4 5 6 7 8 9 10

 marked   1 1 1 1 1 1 1 1 1   0 0

15

Depth-rst search

Page 56: Intro Graph Algorithms

7/18/2019 Intro Graph Algorithms

http://slidepdf.com/reader/full/intro-graph-algorithms 56/117

0

1

2

3

4

5   6

7

8

9

10

0

2

3

14

5

8

Stack:   8   | 9 6 7

0 1 2 3 4 5 6 7 8 9 10

 marked   1 1 1 1 1 1 1 1 1 1   0

15

Depth-rst search

Page 57: Intro Graph Algorithms

7/18/2019 Intro Graph Algorithms

http://slidepdf.com/reader/full/intro-graph-algorithms 57/117

0

1

2

3

4

5   6

7

8

9

10

0

2

3

14

5

9

8

Stack:   9   | 6 7

0 1 2 3 4 5 6 7 8 9 10

 marked   1 1 1 1 1 1 1 1 1 1   0

15

Depth-rst search

Page 58: Intro Graph Algorithms

7/18/2019 Intro Graph Algorithms

http://slidepdf.com/reader/full/intro-graph-algorithms 58/117

0

1

2

3

4

5   6

7

8

9

10

0

2

3

14

5

8

9

6

Stack:   6   |

0 1 2 3 4 5 6 7 8 9 10

 marked   1 1 1 1 1 1 1 1 1 1   0

15

Depth-rst search

Page 59: Intro Graph Algorithms

7/18/2019 Intro Graph Algorithms

http://slidepdf.com/reader/full/intro-graph-algorithms 59/117

0

1

2

3

4

5   6

7

8

9

10

0

2

3

14

5

8

9

6

7

Stack:   7   |

0 1 2 3 4 5 6 7 8 9 10

 marked   1 1 1 1 1 1 1 1 1 1   0

15

Depth-rst search

Page 60: Intro Graph Algorithms

7/18/2019 Intro Graph Algorithms

http://slidepdf.com/reader/full/intro-graph-algorithms 60/117

0

1

2

3

4

5   6

7

8

9

10

0

2

3

14

5

8

9

6

7

Stack:   7   |

0 1 2 3 4 5 6 7 8 9 10

 marked   1 1 1 1 1 1 1 1 1 1   0

15

Depth-rst search

Page 61: Intro Graph Algorithms

7/18/2019 Intro Graph Algorithms

http://slidepdf.com/reader/full/intro-graph-algorithms 61/117

0

1

2

3

4

5   6

7

8

9

10

0

2

3

14

5

8

9

6

7

Stack:   7   | 10

0 1 2 3 4 5 6 7 8 9 10

 marked   1 1 1 1 1 1 1 1 1 1 1

15

Depth-rst search

Page 62: Intro Graph Algorithms

7/18/2019 Intro Graph Algorithms

http://slidepdf.com/reader/full/intro-graph-algorithms 62/117

0

1

2

3

4

5   6

7

8

9

10

0

2

3

14

5

8

9

6

7

10

Stack:   10   |

0 1 2 3 4 5 6 7 8 9 10

 marked   1 1 1 1 1 1 1 1 1 1 1

15

Depth-rst search

Page 63: Intro Graph Algorithms

7/18/2019 Intro Graph Algorithms

http://slidepdf.com/reader/full/intro-graph-algorithms 63/117

0

1

2

3

4

5   6

7

8

9

10

0

2

3

14

5

8

9

6

7

10

Stack: |

0 1 2 3 4 5 6 7 8 9 10

 marked   1 1 1 1 1 1 1 1 1 1 1

15

Depth-rst searchi dj[1000]

Page 64: Intro Graph Algorithms

7/18/2019 Intro Graph Algorithms

http://slidepdf.com/reader/full/intro-graph-algorithms 64/117

vector<int>   adj[1000];

vector<bool>   visited(1000, false);

void   dfs(int   u) {

if   (visited[u]) {

return;

}

visited[u]   =   true;

for   (int   i   =   0; i   <   adj[u].size(); i++) {

int   v   =   adj[u][i];dfs(v);

}

}

16

Connected components

Page 65: Intro Graph Algorithms

7/18/2019 Intro Graph Algorithms

http://slidepdf.com/reader/full/intro-graph-algorithms 65/117

▶  An undirected graph can be partitioned intoconnected components

▶   A connected component is a maximal subset of thevertices such that each pair of vertices is reachable

from each other

▶  We’ve already seen this in a couple of problems, butwe’ve been using Union-Find to keep track of thecomponents

17

Connected components

Page 66: Intro Graph Algorithms

7/18/2019 Intro Graph Algorithms

http://slidepdf.com/reader/full/intro-graph-algorithms 66/117

12

3

4 5

6

7

18

Connected components

Page 67: Intro Graph Algorithms

7/18/2019 Intro Graph Algorithms

http://slidepdf.com/reader/full/intro-graph-algorithms 67/117

12

3

4 5

6

7

18

Connected components

Page 68: Intro Graph Algorithms

7/18/2019 Intro Graph Algorithms

http://slidepdf.com/reader/full/intro-graph-algorithms 68/117

▶   Also possible to nd these components usingdepth-rst search

▶  Pick some vertex we don’t know anything about, anddo a depth-rst search out from it

▶  All vertices reachable from that starting vertex are inthe same component

▶   Repeat this process until you have all the components▶  Time complexity is O(n + m)

19

Connected componentsvector<int>   adj[1000];

Page 69: Intro Graph Algorithms

7/18/2019 Intro Graph Algorithms

http://slidepdf.com/reader/full/intro-graph-algorithms 69/117

j

vector<int>   component(1000,   -1);

void   find_component(int   cur_comp,   int   u) {if   (component[u]   != -1) {

return;

}

component[u]   =   cur_comp;

for   (int   i   =   0; i   <   adj[u].size(); i++) {int   v   =   adj[u][i];

find_component(cur_comp, v);

}

}

int   components   =   0;

for   (int   u   =   0; u   <   n; u++) {if   (component[u]   == -1) {

find_component(components, u);

components++;

}

}

20

Depth-rst search tree

Page 70: Intro Graph Algorithms

7/18/2019 Intro Graph Algorithms

http://slidepdf.com/reader/full/intro-graph-algorithms 70/117

▶   When we do a depth-rst search from a certainvertex, the path that we take forms a tree

▶  When we go from a vertex to another vertex that wehaven’t visited before, the edge that we take is called

a forward edge▶  When we go from a vertex to another vertex that

we’ve already visited before, the edge that we take iscalled a backward edge

▶  To be more specic: the forward edges form a tree

▶  see example

21

Depth-rst search tree

Page 71: Intro Graph Algorithms

7/18/2019 Intro Graph Algorithms

http://slidepdf.com/reader/full/intro-graph-algorithms 71/117

▶  This tree of forward edges, along with the backwardedges, can be analyzed to get a lot of informationabout the original graph

 For example: a backward edge represents a cycle inthe original graph▶  If there are no backward edges, then there are no

cycles in the original graph (i.e. the graph is acyclic)

22

Analyzing the DFS tree

Page 72: Intro Graph Algorithms

7/18/2019 Intro Graph Algorithms

http://slidepdf.com/reader/full/intro-graph-algorithms 72/117

▶  Let’s take a closer look at the depth-rst search tree

▶   First, let’s number each of the vertices in the orderthat we visit them in the depth-rst search

▶  For each vertex, we want to know the smallest

number of a vertex that we visited when exploring thesubtree rooted at the current vertex

▶  Why? We’ll see in a bit..

▶  see example

23

Analyzing the DFS treeconst int   n   =   1000;

vector<int> adj[n];

Page 73: Intro Graph Algorithms

7/18/2019 Intro Graph Algorithms

http://slidepdf.com/reader/full/intro-graph-algorithms 73/117

vector<int>   adj[n];

vector<int>   low(n), num(n,   -1);

int   curnum   =   0;

void   analyze(int   u,   int   p) {

low[u]   =   num[u]   =   curnum++;

for   (int   i   =   0; i   <   adj[u].size(); i++) {

int   v   =   adj[u][i];

if   (v   ==   p)   continue;

if   (num[v]   == -1) {

analyze(v, u);

low[u]   =   min(low[u], low[v]);

}   else   {

low[u]   =   min(low[u], num[v]);

}

}

}

for   (int   u   =   0; u   <   n; u++) {

if   (num[u]   == -1) {

analyze(u,   -1);

}

}

24

Analyzing the DFS tree

Page 74: Intro Graph Algorithms

7/18/2019 Intro Graph Algorithms

http://slidepdf.com/reader/full/intro-graph-algorithms 74/117

▶   Time complexity of this is just O(n + m), since this isbasically just one depth-rst search

▶  Now, as promised, let’s see some applications of this

25

Bridges

Page 75: Intro Graph Algorithms

7/18/2019 Intro Graph Algorithms

http://slidepdf.com/reader/full/intro-graph-algorithms 75/117

▶  We have an undirected graph▶  Without loss of generality, assume it is connected (i.e.

one big connected component)▶   Find an edge, so that if you remove that edge the

graph is no longer connected

▶  Naive algorithm: Try removing edges, one at a time,and nding the connected components of theresulting graph

▶  That’s pretty ineicient, O(m(n + m))

26

Bridges

Page 76: Intro Graph Algorithms

7/18/2019 Intro Graph Algorithms

http://slidepdf.com/reader/full/intro-graph-algorithms 76/117

▶  Let’s take a look at the values that we computed inthe DFS tree

▶   We see that a forward edge (u, v ) is a bridge if andonly if low[v] >num[u]

▶  Simple to extend our analyze function to return allbridges

▶  Again, this is just O(n + m)

27

Bridgesconst int   n   =   1000;vector<int>   adj[n];

Page 77: Intro Graph Algorithms

7/18/2019 Intro Graph Algorithms

http://slidepdf.com/reader/full/intro-graph-algorithms 77/117

jvector<int>   low(n), num(n,   -1);int   curnum   =   0;

vector<pair<int,   int> >   bridges;

void   find_bridges(int   u,   int   p) {low[u]   =   num[u]   =   curnum++;for   (int   i   =   0; i   <   adj[u].size(); i++) {

int   v   =   adj[u][i];if   (v   ==   p)   continue;if   (num[v]   == -1) {

find_bridges(v, u);

low[u]   =   min(low[u], low[v]);}   else   {

low[u]   =   min(low[u], num[v]);}

if   (low[v]   >   num[u]) {bridges.push_back(make_pair(u, v));

}}

}

for   (int   u   =   0; u   <   n; u++) {if   (num[u]   == -1) {

find_bridges(u,  -1);}

}

28

Strongly connected components

W k h d d

Page 78: Intro Graph Algorithms

7/18/2019 Intro Graph Algorithms

http://slidepdf.com/reader/full/intro-graph-algorithms 78/117

▶  We know how to nd connected components in

undirected graphs▶  But what about directed graphs?

▶  Such components behave a bit dierently in directedgraphs, especially since if v  is reachable from u, itdoesn’t mean that u is reachable from v 

▶  The denition remains the same, though▶  A strongly connected component is a maximal subset

of the vertices such that each pair of vertices isreachable from each other

29

Strongly connected components

Page 79: Intro Graph Algorithms

7/18/2019 Intro Graph Algorithms

http://slidepdf.com/reader/full/intro-graph-algorithms 79/117

▶  The connected components algorithm won’t workhere

▶   Instead we can use the depth-rst search tree of the

graph to nd these components

▶  see example

30

Strongly connected componentsvector<int>   adj[100];

( ) ( )

Page 80: Intro Graph Algorithms

7/18/2019 Intro Graph Algorithms

http://slidepdf.com/reader/full/intro-graph-algorithms 80/117

vector<int>   low(100), num(100,   -1);

vector<bool>   incomp(100, false);

int   curnum   =   0;

stack<int>   comp;

void   scc(int   u) {

// scc code...

}

for   (int   i   =   0; i   <   n; i++) {

if   (num[i]   == -1) {scc(i);

}

}

31

Strongly connected componentsvoid   scc(int   u) {

comp push(u);

Page 81: Intro Graph Algorithms

7/18/2019 Intro Graph Algorithms

http://slidepdf.com/reader/full/intro-graph-algorithms 81/117

comp.push(u);incomp[u]   =   true;

low[u]   =   num[u]   =   curnum++;for   (int   i   =   0; i   <   adj[u].size(); i++) {

int   v   =   adj[u][i];if   (num[v]   == -1) {

scc(v);low[u]   =   min(low[u], low[v]);

}   else if   (incomp[v]) {low[u]   =   min(low[u], num[v]);

}

}

if   (num[u]   ==   low[u]) {printf("comp: ");while   (true) {

int   cur   =   comp.top();comp.pop();incomp[cur]   =   false;printf("%d, ", cur);

if   (cur   ==   u) {break;}

}

printf("\n");}

}

32

Strongly connected components

Page 82: Intro Graph Algorithms

7/18/2019 Intro Graph Algorithms

http://slidepdf.com/reader/full/intro-graph-algorithms 82/117

▶  Time complexity?▶  Basically just the DFS analyze function (which was

O(n + m)), with one additional loop to construct thecomponent

▶  But each vertex is only in one component...▶  Time complexity still just O(n + m)

33

Example problem: Come and Go▶   http://uva.onlinejudge.org/external/118/11838.html

Page 83: Intro Graph Algorithms

7/18/2019 Intro Graph Algorithms

http://slidepdf.com/reader/full/intro-graph-algorithms 83/117

34

Topological sort

▶  We have n tasks

Page 84: Intro Graph Algorithms

7/18/2019 Intro Graph Algorithms

http://slidepdf.com/reader/full/intro-graph-algorithms 84/117

▶  Each task i  has a list of tasks that must be nishedbefore we can start task  i 

▶   Find an order in which we can process the tasks

▶  Can be represented as a directed graph– Each task is a vertex in the graph– If task j  should be nished before task  i , then we add a

directed edge from vertex i  to vertex j 

▶   Notice that this can’t be solved if the graph contains acycle

▶   A modied depth-rst search can be used to nd anordering in O(n + m) time, or determine that one doesnot exist

35

Topological sortvector<int>   adj[1000];

vector<bool>   visited(1000, false);

Page 85: Intro Graph Algorithms

7/18/2019 Intro Graph Algorithms

http://slidepdf.com/reader/full/intro-graph-algorithms 85/117

vector<int>   order;

void   topsort(int   u) {

if   (visited[u]) {

return;

}

visited[u]   =   true;

for   (int   i   =   0; i   <   adj[u].size(); i++) {int   v   =   adj[u][i];

topsort(v);

}

order.push_back(u);

}

for   (int   u   =   0; u   <   n; u++) {

topsort(u);

}

36

Example problem: Ordering Tasks▶   http://uva.onlinejudge.org/external/103/10305.html

Page 86: Intro Graph Algorithms

7/18/2019 Intro Graph Algorithms

http://slidepdf.com/reader/full/intro-graph-algorithms 86/117

37

Breadth-rst search

Page 87: Intro Graph Algorithms

7/18/2019 Intro Graph Algorithms

http://slidepdf.com/reader/full/intro-graph-algorithms 87/117

▶  There’s another search algorithm called Breadth-rstsearch

▶   Only dierence is the order in which it visits thevertices

▶   It goes in order of increasing distance from thesource vertex

38

Breadth-rst search

8

Page 88: Intro Graph Algorithms

7/18/2019 Intro Graph Algorithms

http://slidepdf.com/reader/full/intro-graph-algorithms 88/117

0

1

2

3

4

5   6

7

9

10

Queue:

0 1 2 3 4 5 6 7 8 9 10

 marked 0 0 0 0 0 0 0 0 0 0 0

39

Breadth-rst search

8

Page 89: Intro Graph Algorithms

7/18/2019 Intro Graph Algorithms

http://slidepdf.com/reader/full/intro-graph-algorithms 89/117

0

1

2

3

4

5   6

7

9

10

0

Queue:   0

0 1 2 3 4 5 6 7 8 9 10

 marked   1   0 0 0 0 0 0 0 0 0 0

39

Breadth-rst search

8

Page 90: Intro Graph Algorithms

7/18/2019 Intro Graph Algorithms

http://slidepdf.com/reader/full/intro-graph-algorithms 90/117

0

1

2

3

4

5   6

7

9

10

0

Queue:   0

0 1 2 3 4 5 6 7 8 9 10

 marked   1   0 0 0 0 0 0 0 0 0 0

39

Breadth-rst search

8

Page 91: Intro Graph Algorithms

7/18/2019 Intro Graph Algorithms

http://slidepdf.com/reader/full/intro-graph-algorithms 91/117

0

1

2

3

4

5   6

7

9

10

0

Queue:   0   1 2

0 1 2 3 4 5 6 7 8 9 10

 marked   1 1 1   0 0 0 0 0 0 0 0

39

Breadth-rst search

8

Page 92: Intro Graph Algorithms

7/18/2019 Intro Graph Algorithms

http://slidepdf.com/reader/full/intro-graph-algorithms 92/117

0

1

2

3

4

5   6

7

9

10

0

1

Queue:   1   2

0 1 2 3 4 5 6 7 8 9 10

 marked   1 1 1   0 0 0 0 0 0 0 0

39

Breadth-rst search

38

Page 93: Intro Graph Algorithms

7/18/2019 Intro Graph Algorithms

http://slidepdf.com/reader/full/intro-graph-algorithms 93/117

0

1

2

3

4

5   6

7

9

10

0

1

Queue:   1   2

0 1 2 3 4 5 6 7 8 9 10

 marked   1 1 1   0 0 0 0 0 0 0 0

39

Breadth-rst search

38

Page 94: Intro Graph Algorithms

7/18/2019 Intro Graph Algorithms

http://slidepdf.com/reader/full/intro-graph-algorithms 94/117

0

1

2

3

4

5   6

7

9

10

0

1

Queue:   1   2 4

0 1 2 3 4 5 6 7 8 9 10

 marked   1 1 1   0   1   0 0 0 0 0 0

39

Breadth-rst search

38

Page 95: Intro Graph Algorithms

7/18/2019 Intro Graph Algorithms

http://slidepdf.com/reader/full/intro-graph-algorithms 95/117

0

1

2

3

4

5   6

7

9

10

0

11

22

Queue:   2   4

0 1 2 3 4 5 6 7 8 9 10

 marked   1 1 1   0   1   0 0 0 0 0 0

39

Breadth-rst search

38

Page 96: Intro Graph Algorithms

7/18/2019 Intro Graph Algorithms

http://slidepdf.com/reader/full/intro-graph-algorithms 96/117

0

1

2

3

4

5   6

7

9

10

0

11

22

Queue:   2   4

0 1 2 3 4 5 6 7 8 9 10

 marked   1 1 1   0   1   0 0 0 0 0 0

39

Breadth-rst search

38

Page 97: Intro Graph Algorithms

7/18/2019 Intro Graph Algorithms

http://slidepdf.com/reader/full/intro-graph-algorithms 97/117

0

1

2

3

4

5   6

7

9

10

0

11

22

Queue:   2   4 3

0 1 2 3 4 5 6 7 8 9 10

 marked   1 1 1 1 1   0 0 0 0 0 0

39

Breadth-rst search

38

Page 98: Intro Graph Algorithms

7/18/2019 Intro Graph Algorithms

http://slidepdf.com/reader/full/intro-graph-algorithms 98/117

0

1

2

3

4

5   6

7

9

10

0

11

2

4

Queue:   4   3

0 1 2 3 4 5 6 7 8 9 10

 marked   1 1 1 1 1   0 0 0 0 0 0

39

Breadth-rst search

38

Page 99: Intro Graph Algorithms

7/18/2019 Intro Graph Algorithms

http://slidepdf.com/reader/full/intro-graph-algorithms 99/117

0

1

2

3

4

5   6

7

9

10

0

11

2

4

Queue:   4   3

0 1 2 3 4 5 6 7 8 9 10

 marked   1 1 1 1 1   0 0 0 0 0 0

39

Breadth-rst search

38

Page 100: Intro Graph Algorithms

7/18/2019 Intro Graph Algorithms

http://slidepdf.com/reader/full/intro-graph-algorithms 100/117

0

1

2

3

4

5   6

7

9

10

0

11

2

4

Queue:   4   3 5

0 1 2 3 4 5 6 7 8 9 10

 marked   1 1 1 1 1 1   0 0 0 0 0

39

Breadth-rst search

38

3

3

Page 101: Intro Graph Algorithms

7/18/2019 Intro Graph Algorithms

http://slidepdf.com/reader/full/intro-graph-algorithms 101/117

0

1

2

3

4

5   6

7

9

10

0

11

2

4

33

Queue:   3   5

0 1 2 3 4 5 6 7 8 9 10

 marked   1 1 1 1 1 1   0 0 0 0 0

39

Breadth-rst search

38

3

Page 102: Intro Graph Algorithms

7/18/2019 Intro Graph Algorithms

http://slidepdf.com/reader/full/intro-graph-algorithms 102/117

0

1

2

3

4

5   6

7

9

10

0

11

2

4

3

5

Queue:   5

0 1 2 3 4 5 6 7 8 9 10

 marked   1 1 1 1 1 1   0 0 0 0 0

39

Breadth-rst search

38

3

Page 103: Intro Graph Algorithms

7/18/2019 Intro Graph Algorithms

http://slidepdf.com/reader/full/intro-graph-algorithms 103/117

0

1

2

3

4

5   6

7

9

10

0

11

2

4

3

5

Queue:   5

0 1 2 3 4 5 6 7 8 9 10

 marked   1 1 1 1 1 1   0 0 0 0 0

39

Breadth-rst search

38

3

Page 104: Intro Graph Algorithms

7/18/2019 Intro Graph Algorithms

http://slidepdf.com/reader/full/intro-graph-algorithms 104/117

0

1

2

4

5   6

7

9

10

0

11

2

4

5

Queue:   5   6 7 8

0 1 2 3 4 5 6 7 8 9 10

 marked   1 1 1 1 1 1 1 1 1   0 0

39

Breadth-rst search

38

3

Page 105: Intro Graph Algorithms

7/18/2019 Intro Graph Algorithms

http://slidepdf.com/reader/full/intro-graph-algorithms 105/117

0

1

2

4

5   6

7

9

10

0

11

2

4

5   6

Queue:   6   7 8

0 1 2 3 4 5 6 7 8 9 10

 marked   1 1 1 1 1 1 1 1 1   0 0

39

Breadth-rst search

38

3

Page 106: Intro Graph Algorithms

7/18/2019 Intro Graph Algorithms

http://slidepdf.com/reader/full/intro-graph-algorithms 106/117

0

1

2

4

5   6

7

9

10

0

11

2

4

5   6

7

Queue:   7   8

0 1 2 3 4 5 6 7 8 9 10

 marked   1 1 1 1 1 1 1 1 1   0 0

39

Breadth-rst search

38

3

Page 107: Intro Graph Algorithms

7/18/2019 Intro Graph Algorithms

http://slidepdf.com/reader/full/intro-graph-algorithms 107/117

0

1

2

4

5   6

7

9

10

0

11

2

4

5   6

7

Queue:   7   8

0 1 2 3 4 5 6 7 8 9 10

 marked   1 1 1 1 1 1 1 1 1   0 0

39

Breadth-rst search

38

3

Page 108: Intro Graph Algorithms

7/18/2019 Intro Graph Algorithms

http://slidepdf.com/reader/full/intro-graph-algorithms 108/117

0

1

2

4

5   6

7

9

10

0

11

2

4

5   6

7

Queue:   7   8 10

0 1 2 3 4 5 6 7 8 9 10

 marked   1 1 1 1 1 1 1 1 1   0   1

39

Breadth-rst search

38

38

Page 109: Intro Graph Algorithms

7/18/2019 Intro Graph Algorithms

http://slidepdf.com/reader/full/intro-graph-algorithms 109/117

0

1

2

4

5   6

7

9

10

0

11

2

4

5   6

7

Queue:   8   10

0 1 2 3 4 5 6 7 8 9 10

 marked   1 1 1 1 1 1 1 1 1   0   1

39

Breadth-rst search

38

38

Page 110: Intro Graph Algorithms

7/18/2019 Intro Graph Algorithms

http://slidepdf.com/reader/full/intro-graph-algorithms 110/117

0

1

2

4

5   6

7

9

10

0

11

2

4

5   6

7

Queue:   8   10

0 1 2 3 4 5 6 7 8 9 10

 marked   1 1 1 1 1 1 1 1 1   0   1

39

Breadth-rst search

38

38

Page 111: Intro Graph Algorithms

7/18/2019 Intro Graph Algorithms

http://slidepdf.com/reader/full/intro-graph-algorithms 111/117

0

1

2

4

5   6

7

9

10

0

11

2

4

5   6

7

Queue:   8   10 9

0 1 2 3 4 5 6 7 8 9 10

 marked   1 1 1 1 1 1 1 1 1 1 1

39

Breadth-rst search

38

38

Page 112: Intro Graph Algorithms

7/18/2019 Intro Graph Algorithms

http://slidepdf.com/reader/full/intro-graph-algorithms 112/117

0

1

2

4

5   6

7

9

10

0

11

2

4

5   6

7

10

Queue:   10   9

0 1 2 3 4 5 6 7 8 9 10

 marked   1 1 1 1 1 1 1 1 1 1 1

39

Breadth-rst search

38

38

Page 113: Intro Graph Algorithms

7/18/2019 Intro Graph Algorithms

http://slidepdf.com/reader/full/intro-graph-algorithms 113/117

0

1

2

4

5   6

7

9

10

0

11

2

4

5   6

7

10

9

Queue:   9

0 1 2 3 4 5 6 7 8 9 10

 marked   1 1 1 1 1 1 1 1 1 1 1

39

Breadth-rst search

38

38

Page 114: Intro Graph Algorithms

7/18/2019 Intro Graph Algorithms

http://slidepdf.com/reader/full/intro-graph-algorithms 114/117

0

1

2

4

5   6

7

9

10

0

11

2

4

5   6

7

10

9

Queue:

0 1 2 3 4 5 6 7 8 9 10

 marked   1 1 1 1 1 1 1 1 1 1 1

39

Breadth-rst searchvector<int>   adj[1000];

vector<bool>   visited(1000, false);

Page 115: Intro Graph Algorithms

7/18/2019 Intro Graph Algorithms

http://slidepdf.com/reader/full/intro-graph-algorithms 115/117

queue<int>   Q;Q.push(start);

visited[start]   =   true;

while   (!Q.empty()) {

int   u   =   Q.front(); Q.pop();

for   (int   i   =   0; i   <   adj[u].size(); i++) {

int   v   =   adj[u][i];

if   (!visited[v]) {

Q.push(v);

visited[v]   =   true;}

}

}

40

Shortest path in unweighted graphs▶  We have an unweighted graph, and want to nd the

shortest path from A to B

Page 116: Intro Graph Algorithms

7/18/2019 Intro Graph Algorithms

http://slidepdf.com/reader/full/intro-graph-algorithms 116/117

▶   That is, we want to nd a path from A to B with theminimum number of edges

▶  Breadth-rst search goes through the vertices in

increasing order of distance from the start vertex▶  Just do a single breadth-rst search from  A, until we

nd B

▶  Or let the search continue through the whole graph,

and then we have the shortest paths from A to allother vertices

▶  Shortest path from A to all other vertices:  O(n + m)

41

Shortest path in unweighted graphsvector<int>   adj[1000];

vector<bool>   dist(1000,   -1);

queue<int>   Q;

Page 117: Intro Graph Algorithms

7/18/2019 Intro Graph Algorithms

http://slidepdf.com/reader/full/intro-graph-algorithms 117/117

Q.push(A);dist[A]   =   0;

while   (!Q.empty()) {

int   u   =   Q.front(); Q.pop();

for   (int   i   =   0; i   <   adj[u].size(); i++) {

int   v   =   adj[u][i];if   (dist[v]   == -1) {

Q.push(v);

dist[v]   =   1   +   dist[u];

}

}

}

printf("%d\n", dist[B]);

42