191
POSCAT Seminar 9 : Graph 1 yougatup @ POSCAT 1

Poscat seminar 9

Embed Size (px)

Citation preview

Page 1: Poscat seminar 9

POSCAT Seminar 9 :Graph 1

yougatup @ POSCAT

1

Page 2: Poscat seminar 9

Topic

Topic today

− Basic concept

− Graph representation

• Adjacency matrix

• Adjacency list

• Tradeoffs

− Graph Traversal

• Depth First Search

• Breadth First Search

• Flood Fill

• Connected Component

• Strongly Connected Component

POSCAT Seminar 1-214 July 2014

yougatup

Page 3: Poscat seminar 9

Graph

Basic Concept

− 𝐺 = (𝑉, 𝐸)

− A set of vertices and edges

POSCAT Seminar 1-314 July 2014

yougatup

1 2

3

4

6

5

7

8

Page 4: Poscat seminar 9

Graph

Terminology

− Nodes

POSCAT Seminar 1-414 July 2014

yougatup

1 2

3

4

6

5

7

8

Page 5: Poscat seminar 9

Graph

Terminology

− Edges

POSCAT Seminar 1-514 July 2014

yougatup

1 2

3

4

6

5

7

8

Page 6: Poscat seminar 9

Graph

Basic Concept

− 𝐷𝑒𝑔𝑟𝑒𝑒(𝑣) : the number of adjacent edges for a vertex 𝑣

POSCAT Seminar 1-614 July 2014

yougatup

1 2

3

4

6

5

7

8

32

2

3 2

32

1

Page 7: Poscat seminar 9

Graph

Basic Concept

− 𝐶𝑜𝑛𝑛𝑒𝑐𝑡𝑒𝑑𝑛𝑒𝑠𝑠 : a graph is connected if there exist always a

path which connects 𝑣 and 𝑤 for all 𝑣,𝑤

POSCAT Seminar 1-714 July 2014

yougatup

1 2

3

4

6

5

7

8

Page 8: Poscat seminar 9

Graph Representation

Representation

− How can we represent a graph in the computer ?

− Adjacency matrix & list

POSCAT Seminar 1-814 July 2014

yougatup

1 2

3

4

6

5

7

8

Page 9: Poscat seminar 9

Graph Representation

Adjacency Matrix

− Use matrix ! ( you may already know this )

− 𝐺𝑖𝑗 = 1 𝑖𝑓 𝑡ℎ𝑒𝑟𝑒 𝑖𝑠 𝑎 𝑒𝑑𝑔𝑒 𝑏𝑒𝑡𝑤𝑒𝑒𝑛 𝑣𝑒𝑟𝑡𝑒𝑥 𝑖 𝑎𝑛𝑑 𝑗

0 𝑜𝑡ℎ𝑒𝑟𝑤𝑖𝑠𝑒

POSCAT Seminar 1-914 July 2014

yougatup

1 2

3

4

6

5

7

8

Page 10: Poscat seminar 9

Graph Representation

Adjacency Matrix

− Use matrix ! ( you may already know this )

− 𝐺𝑖𝑗 = 1 𝑖𝑓 𝑡ℎ𝑒𝑟𝑒 𝑖𝑠 𝑎 𝑒𝑑𝑔𝑒 𝑏𝑒𝑡𝑤𝑒𝑒𝑛 𝑣𝑒𝑟𝑡𝑒𝑥 𝑖 𝑎𝑛𝑑 𝑗

0 𝑜𝑡ℎ𝑒𝑟𝑤𝑖𝑠𝑒

POSCAT Seminar 1-1014 July 2014

yougatup

1 2

3

4

6

5

7

8

1 2 3 4 5 6 7 8

1 0 1 1 0 0 0 0 0

2 1 0 1 1 0 0 0 0

3 1 1 0 0 0 0 0 0

4 0 1 0 0 1 1 0 0

5 0 0 0 1 0 0 1 0

6 0 0 0 1 0 0 1 0

7 0 0 0 0 1 1 0 1

8 0 0 0 0 0 0 1 0

Page 11: Poscat seminar 9

Graph Representation

Adjacency Matrix

− Use matrix ! ( you may already know this )

− 𝐺𝑖𝑗 = 1 𝑖𝑓 𝑡ℎ𝑒𝑟𝑒 𝑖𝑠 𝑎 𝑒𝑑𝑔𝑒 𝑏𝑒𝑡𝑤𝑒𝑒𝑛 𝑣𝑒𝑟𝑡𝑒𝑥 𝑖 𝑎𝑛𝑑 𝑗

0 𝑜𝑡ℎ𝑒𝑟𝑤𝑖𝑠𝑒

POSCAT Seminar 1-1114 July 2014

yougatup

1 2

3

4

6

5

7

8

1 2 3 4 5 6 7 8

1 0 1 1 0 0 0 0 0

2 1 0 1 1 0 0 0 0

3 1 1 0 0 0 0 0 0

4 0 1 0 0 1 1 0 0

5 0 0 0 1 0 0 1 0

6 0 0 0 1 0 0 1 0

7 0 0 0 0 1 1 0 1

8 0 0 0 0 0 0 1 0

Is it good ?

Page 12: Poscat seminar 9

Graph Representation

Adjacency Matrix

− We can determine whether there is a edge between i and j directly

POSCAT Seminar 1-1214 July 2014

yougatup

1 2

3

4

6

5

7

8

1 2 3 4 5 6 7 8

1 0 1 1 0 0 0 0 0

2 1 0 1 1 0 0 0 0

3 1 1 0 0 0 0 0 0

4 0 1 0 0 1 1 0 0

5 0 0 0 1 0 0 1 0

6 0 0 0 1 0 0 1 0

7 0 0 0 0 1 1 0 1

8 0 0 0 0 0 0 1 0

Page 13: Poscat seminar 9

Graph Representation

Adjacency Matrix

− We can determine whether there is a edge between i and j directly

POSCAT Seminar 1-1314 July 2014

yougatup

1 2

3

4

6

5

7

8

1 2 3 4 5 6 7 8

1 0 1 1 0 0 0 0 0

2 1 0 1 1 0 0 0 0

3 1 1 0 0 0 0 0 0

4 0 1 0 0 1 1 0 0

5 0 0 0 1 0 0 1 0

6 0 0 0 1 0 0 1 0

7 0 0 0 0 1 1 0 1

8 0 0 0 0 0 0 1 0Is there a edge between 3 and 4 ?

Page 14: Poscat seminar 9

Graph Representation

Adjacency Matrix

− We can determine whether there is a edge between i and j directly

POSCAT Seminar 1-1414 July 2014

yougatup

1 2

3

4

6

5

7

8

1 2 3 4 5 6 7 8

1 0 1 1 0 0 0 0 0

2 1 0 1 1 0 0 0 0

3 1 1 0 0 0 0 0 0

4 0 1 0 0 1 1 0 0

5 0 0 0 1 0 0 1 0

6 0 0 0 1 0 0 1 0

7 0 0 0 0 1 1 0 1

8 0 0 0 0 0 0 1 0Is there a edge between 3 and 4 ? No!

Page 15: Poscat seminar 9

Graph Representation

Adjacency Matrix

− We can determine whether there is a edge between i and j directly

− We have to maintain whole matrix to save a graph

POSCAT Seminar 1-1514 July 2014

yougatup

1 2

3

4

6

5

7

8

1 2 3 4 5 6 7 8

1 0 1 1 0 0 0 0 0

2 1 0 1 1 0 0 0 0

3 1 1 0 0 0 0 0 0

4 0 1 0 0 1 1 0 0

5 0 0 0 1 0 0 1 0

6 0 0 0 1 0 0 1 0

7 0 0 0 0 1 1 0 1

8 0 0 0 0 0 0 1 0

Page 16: Poscat seminar 9

Graph Representation

Adjacency Matrix

− We can determine whether there is a edge between i and j directly

− We have to maintain whole matrix to save a graph

POSCAT Seminar 1-1614 July 2014

yougatup

1 2

3

4

6

5

7

8

1 2 3 4 5 6 7 8

1 0 1 0 0 0 0 0 0

2 1 0 1 1 0 0 0 0

3 0 1 0 0 0 0 0 0

4 0 1 0 0 1 0 0 0

5 0 0 0 1 0 0 1 0

6 0 0 0 0 0 0 1 0

7 0 0 0 0 1 1 0 1

8 0 0 0 0 0 0 1 0How about this ?

Page 17: Poscat seminar 9

Graph Representation

Adjacency Matrix

− We can determine whether there is a edge between i and j directly

− We have to maintain whole matrix to save a graph

POSCAT Seminar 1-1714 July 2014

yougatup

1 2

3

4

6

5

7

8

1 2 3 4 5 6 7 8

1 0 1 0 0 0 0 0 0

2 1 0 1 1 0 0 0 0

3 0 1 0 0 0 0 0 0

4 0 1 0 0 1 0 0 0

5 0 0 0 1 0 0 1 0

6 0 0 0 0 0 0 1 0

7 0 0 0 0 1 1 0 1

8 0 0 0 0 0 0 1 0Compare the number of 1 and 0

Page 18: Poscat seminar 9

Graph Representation

Adjacency Matrix

− We can determine whether there is a edge between i and j directly

− We have to maintain whole matrix to save a graph

POSCAT Seminar 1-1814 July 2014

yougatup

1 2

3

4

6

5

7

8

1 2 3 4 5 6 7 8

1 0 1 0 0 0 0 0 0

2 1 0 1 1 0 0 0 0

3 0 1 0 0 0 0 0 0

4 0 1 0 0 1 0 0 0

5 0 0 0 1 0 0 1 0

6 0 0 0 0 0 0 1 0

7 0 0 0 0 1 1 0 1

8 0 0 0 0 0 0 1 0Compare the number of 1 and 0Can we handle a graph with 100,000 vertex ?

Page 19: Poscat seminar 9

Graph Representation

Adjacency Matrix

− We can determine whether there is a edge between i and j directly

− We have to maintain whole matrix to save a graph

− It takes 𝑂(𝑉) to find all the adjacent vertices for a vertex 𝑣

POSCAT Seminar 1-1914 July 2014

yougatup

1 2

3

4

6

5

7

8

1 2 3 4 5 6 7 8

1 0 1 1 0 0 0 0 0

2 1 0 1 1 0 0 0 0

3 1 1 0 0 0 0 0 0

4 0 1 0 0 1 1 0 0

5 0 0 0 1 0 0 1 0

6 0 0 0 1 0 0 1 0

7 0 0 0 0 1 1 0 1

8 0 0 0 0 0 0 1 0

Page 20: Poscat seminar 9

Graph Representation

Adjacency Matrix

− We can determine whether there is a edge between i and j directly

− We have to maintain whole matrix to save a graph

− It takes 𝑂(𝑉) to find all the adjacent vertices for a vertex 𝑣

POSCAT Seminar 1-2014 July 2014

yougatup

1 2

3

4

6

5

7

8

1 2 3 4 5 6 7 8

1 0 1 1 0 0 0 0 0

2 1 0 1 1 0 0 0 0

3 1 1 0 0 0 0 0 0

4 0 1 0 0 1 1 0 0

5 0 0 0 1 0 0 1 0

6 0 0 0 1 0 0 1 0

7 0 0 0 0 1 1 0 1

8 0 0 0 0 0 0 1 0Give me all the adjacent vertices for vertex 3

Page 21: Poscat seminar 9

Graph Representation

Adjacency Matrix

− We can determine whether there is a edge between i and j directly

− We have to maintain whole matrix to save a graph

− It takes 𝑂(𝑉) to find all the adjacent vertices for a vertex 𝑣

POSCAT Seminar 1-2114 July 2014

yougatup

1 2

3

4

6

5

7

8

1 2 3 4 5 6 7 8

1 0 1 1 0 0 0 0 0

2 1 0 1 1 0 0 0 0

3 1 1 0 0 0 0 0 0

4 0 1 0 0 1 1 0 0

5 0 0 0 1 0 0 1 0

6 0 0 0 1 0 0 1 0

7 0 0 0 0 1 1 0 1

8 0 0 0 0 0 0 1 0Give me all the adjacent vertices for vertex 3 It takes 𝑂(𝑉)

Page 22: Poscat seminar 9

Graph Representation

Adjacency List

− Handle just adjacent vertices for all vertices

POSCAT Seminar 1-2214 July 2014

yougatup

1 2

3

4

6

5

7

8

Page 23: Poscat seminar 9

Graph Representation

Adjacency List

− Handle just adjacent vertices for all vertices

POSCAT Seminar 1-2314 July 2014

yougatup

1 2

3

4

6

5

7

8

2 3

1 3 4

1 2

2 5 6

4 7

4 7

5 6 8

1

2

3

4

5

6

7

Page 24: Poscat seminar 9

Graph Representation

Adjacency List

− Handle just adjacent vertices for all vertices

− Optimal space complexity (i.e. no redundant space)

POSCAT Seminar 1-2414 July 2014

yougatup

1 2

3

4

6

5

7

8

2 3

1 3 4

1 2

2 5 6

4 7

4 7

5 6 8

1

2

3

4

5

6

7

Page 25: Poscat seminar 9

Graph Representation

Adjacency List

− Handle just adjacent vertices for all vertices

− Optimal space complexity (i.e. no redundant space)

− It takes O(𝑑𝑒𝑔𝑟𝑒𝑒(𝑣)) to find all the adjacent vertices for vertex 𝑣

POSCAT Seminar 1-2514 July 2014

yougatup

1 2

3

4

6

5

7

8

2 3

1 3 4

1 2

2 5 6

4 7

4 7

5 6 8

1

2

3

4

5

6

7

Page 26: Poscat seminar 9

Graph Representation

Adjacency List

− Handle just adjacent vertices for all vertices

− Optimal space complexity (i.e. no redundant space)

− It takes O(𝑑𝑒𝑔𝑟𝑒𝑒(𝑣)) to find all the adjacent vertices for vertex 𝑣

− It take also O(𝑑𝑒𝑔𝑟𝑒𝑒(𝑣)) to determine the existence of edge

POSCAT Seminar 1-2614 July 2014

yougatup

1 2

3

4

6

5

7

8

2 3

1 3 4

1 2

2 5 6

4 7

4 7

5 6 8

1

2

3

4

5

6

7

Page 27: Poscat seminar 9

Graph Representation

Adjacency List

− Handle just adjacent vertices for all vertices

− Optimal space complexity (i.e. no redundant space)

− It takes O(𝑑𝑒𝑔𝑟𝑒𝑒(𝑣)) to find all the adjacent vertices for vertex 𝑣

− It take also O(𝑑𝑒𝑔𝑟𝑒𝑒(𝑣)) to determine the existence of edge

POSCAT Seminar 1-2714 July 2014

yougatup

1 2

3

4

6

5

7

8

2 3

1 3 4

1 2

2 5 6

4 7

4 7

5 6 8

1

2

3

4

5

6

7Is there edge between vertex 1 and 3 ?

Page 28: Poscat seminar 9

Graph Representation

Adjacency List

− Handle just adjacent vertices for all vertices

− Optimal space complexity (i.e. no redundant space)

− It takes O(𝑑𝑒𝑔𝑟𝑒𝑒(𝑣)) to find all the adjacent vertices for vertex 𝑣

− It take also O(𝑑𝑒𝑔𝑟𝑒𝑒(𝑣)) to determine the existence of edge

POSCAT Seminar 1-2814 July 2014

yougatup

1 2

3

4

6

5

7

8

2 3

1 3 4

1 2

2 5 6

4 7

4 7

5 6 8

1

2

3

4

5

6

7Is there edge between vertex 1 and 3 ?

We have to consider all of these

Page 29: Poscat seminar 9

Graph Representation

Adjacency List

By the way, is it possible to manage this kind of structure ?

POSCAT Seminar 1-2914 July 2014

yougatup

1 2

3

4

6

5

7

8

2 3

1 3 4

1 2

2 5 6

4 7

4 7

5 6 8

1

2

3

4

5

6

7

Page 30: Poscat seminar 9

Graph Representation

Adjacency List

By the way, is it possible to manage this kind of structure ?

Yes we can! Use STL vector ( I’ll give some simple lecture for you today )

POSCAT Seminar 1-3014 July 2014

yougatup

1 2

3

4

6

5

7

8

2 3

1 3 4

1 2

2 5 6

4 7

4 7

5 6 8

1

2

3

4

5

6

7

Page 31: Poscat seminar 9

Graph Representation

Summary

− Adjacency matrix VS Adjacency list

− Existence of edge ?

− Find all the neighboring vertices ?

− Space ?

POSCAT Seminar 1-3114 July 2014

yougatup

1 2

3

4

6

5

7

8

Page 32: Poscat seminar 9

Graph Representation

Summary

− Adjacency matrix VS Adjacency list

− Existence of edge ?

− Find all the neighboring vertices ?

− Space ?

POSCAT Seminar 1-3214 July 2014

yougatup

1 2

3

4

6

5

7

8

Which one is better ?

Page 33: Poscat seminar 9

Graph Representation

Summary

− Adjacency matrix VS Adjacency list

− Existence of edge ?

− Find all the neighboring vertices ?

− Space ?

POSCAT Seminar 1-3314 July 2014

yougatup

1 2

3

4

6

5

7

8

Which one is better ?It depends, but usually we prefer adjacency list

Page 34: Poscat seminar 9

Graph Representation

Summary

− Adjacency matrix VS Adjacency list

− Existence of edge ?

− Find all the neighboring vertices ?

− Space ?

POSCAT Seminar 1-3414 July 2014

yougatup

1 2

3

4

6

5

7

8

Page 35: Poscat seminar 9

Graph Traversal

Traversal paradigm

− Basically, graph is also data structure

− In other words, we want to contain some information into a graph

− Therefore, we have to be able to traverse whole graph to find datas

− Depth First Search VS Breadth First Search

POSCAT Seminar 1-3514 July 2014

yougatup

1 2

3

4

6

5

7

8

Page 36: Poscat seminar 9

Graph Traversal

Depth First Search

− Use stack to traverse

− Invarient : The current vertex is pointed by the top of the stack

( Discuss it later. Forget it )

POSCAT Seminar 1-3614 July 2014

yougatup

1 2

3

4

6

5

7

8

Page 37: Poscat seminar 9

Graph Traversal

Depth First Search

− Procedure

1. Select a start vertex

2. Move to adjacent vertex we don’t visit yet

3. If there is no such a vertex, move backward

4. Go to step 2.

POSCAT Seminar 1-3714 July 2014

yougatup

1 2

3

4

6

5

7

8

Page 38: Poscat seminar 9

Graph Traversal

Depth First Search

− Procedure

1. Select a start vertex

2. Move to adjacent vertex we don’t visit yet

3. If there is no such a vertex, move backward

4. Go to step 2.

POSCAT Seminar 1-3814 July 2014

yougatup

1 2

3

4

6

5

7

8

Page 39: Poscat seminar 9

Graph Traversal

Depth First Search

− Procedure

1. Select a start vertex

2. Move to adjacent vertex we don’t visit yet

3. If there is no such a vertex, move backward

4. Go to step 2.

POSCAT Seminar 1-3914 July 2014

yougatup

1 2

3

4

6

5

7

8

Page 40: Poscat seminar 9

Graph Traversal

Depth First Search

− Procedure

1. Select a start vertex

2. Move to adjacent vertex we don’t visit yet

3. If there is no such a vertex, move backward

4. Go to step 2.

POSCAT Seminar 1-4014 July 2014

yougatup

1 2

3

4

6

5

7

8

Page 41: Poscat seminar 9

Graph Traversal

Depth First Search

− Procedure

1. Select a start vertex

2. Move to adjacent vertex we don’t visit yet

3. If there is no such a vertex, move backward

4. Go to step 2.

POSCAT Seminar 1-4114 July 2014

yougatup

1 2

3

4

6

5

7

8

Page 42: Poscat seminar 9

Graph Traversal

Depth First Search

− Procedure

1. Select a start vertex

2. Move to adjacent vertex we don’t visit yet

3. If there is no such a vertex, move backward

4. Go to step 2.

POSCAT Seminar 1-4214 July 2014

yougatup

1 2

3

4

6

5

7

8

Page 43: Poscat seminar 9

Graph Traversal

Depth First Search

− Procedure

1. Select a start vertex

2. Move to adjacent vertex we don’t visit yet

3. If there is no such a vertex, move backward

4. Go to step 2.

POSCAT Seminar 1-4314 July 2014

yougatup

1 2

3

4

6

5

7

8

Page 44: Poscat seminar 9

Graph Traversal

Depth First Search

− Procedure

1. Select a start vertex

2. Move to adjacent vertex we don’t visit yet

3. If there is no such a vertex, move backward

4. Go to step 2.

POSCAT Seminar 1-4414 July 2014

yougatup

1 2

3

4

6

5

7

8

Page 45: Poscat seminar 9

Graph Traversal

Depth First Search

− Procedure

1. Select a start vertex

2. Move to adjacent vertex we don’t visit yet

3. If there is no such a vertex, move backward

4. Go to step 2.

POSCAT Seminar 1-4514 July 2014

yougatup

1 2

3

4

6

5

7

8

Page 46: Poscat seminar 9

Graph Traversal

Depth First Search

− Procedure

1. Select a start vertex

2. Move to adjacent vertex we don’t visit yet

3. If there is no such a vertex, move backward

4. Go to step 2.

POSCAT Seminar 1-4614 July 2014

yougatup

1 2

3

4

6

5

7

8

Page 47: Poscat seminar 9

Graph Traversal

Depth First Search

− Procedure

1. Select a start vertex

2. Move to adjacent vertex we don’t visit yet

3. If there is no such a vertex, move backward

4. Go to step 2.

POSCAT Seminar 1-4714 July 2014

yougatup

1 2

3

4

6

5

7

8

Page 48: Poscat seminar 9

Graph Traversal

Depth First Search

− Procedure

1. Select a start vertex

2. Move to adjacent vertex we don’t visit yet

3. If there is no such a vertex, move backward

4. Go to step 2.

POSCAT Seminar 1-4814 July 2014

yougatup

1 2

3

4

6

5

7

8

Page 49: Poscat seminar 9

Graph Traversal

Depth First Search

− Procedure

1. Select a start vertex

2. Move to adjacent vertex we don’t visit yet

3. If there is no such a vertex, move backward

4. Go to step 2.

POSCAT Seminar 1-4914 July 2014

yougatup

1 2

3

4

6

5

7

8

Page 50: Poscat seminar 9

Graph Traversal

Depth First Search

− Procedure

1. Select a start vertex

2. Move to adjacent vertex we don’t visit yet

3. If there is no such a vertex, move backward

4. Go to step 2.

POSCAT Seminar 1-5014 July 2014

yougatup

1 2

3

4

6

5

7

8

Page 51: Poscat seminar 9

Graph Traversal

Depth First Search

− Procedure

1. Select a start vertex

2. Move to adjacent vertex we don’t visit yet

3. If there is no such a vertex, move backward

4. Go to step 2.

POSCAT Seminar 1-5114 July 2014

yougatup

1 2

3

4

6

5

7

8

Page 52: Poscat seminar 9

Graph Traversal

Depth First Search

− Procedure

1. Select a start vertex

2. Move to adjacent vertex we don’t visit yet

3. If there is no such a vertex, move backward

4. Go to step 2.

POSCAT Seminar 1-5214 July 2014

yougatup

1 2

3

4

6

5

7

8

Page 53: Poscat seminar 9

Graph Traversal

Depth First Search

− Procedure

1. Select a start vertex

2. Move to adjacent vertex we don’t visit yet

3. If there is no such a vertex, move backward

4. Go to step 2.

POSCAT Seminar 1-5314 July 2014

yougatup

1 2

3

4

6

5

7

8

Page 54: Poscat seminar 9

Graph Traversal

Depth First Search

− Procedure

1. Select a start vertex

2. Move to adjacent vertex we don’t visit yet

3. If there is no such a vertex, move backward

4. Go to step 2.

POSCAT Seminar 1-5414 July 2014

yougatup

1 2

3

4

6

5

7

8Done ! What is the visiting sequence ?

Page 55: Poscat seminar 9

Graph Traversal

Depth First Search

− Procedure

1. Select a start vertex

2. Move to adjacent vertex we don’t visit yet

3. If there is no such a vertex, move backward

4. Go to step 2.

POSCAT Seminar 1-5514 July 2014

yougatup

1 2

3

4

6

5

7

8Well, how can we implement it ?

Page 56: Poscat seminar 9

Graph Traversal

Depth First Search

− Procedure

1. Select a start vertex

2. Move to adjacent vertex we don’t visit yet

3. If there is no such a vertex, move backward

4. Go to step 2.

POSCAT Seminar 1-5614 July 2014

yougatup

1 2

3

4

6

5

7

8Well, how can we implement it ?We have to implement Step 2. and 3.

Page 57: Poscat seminar 9

Graph Traversal

Depth First Search

− Procedure

1. Select a start vertex

2. Move to adjacent vertex we don’t visit yet

3. If there is no such a vertex, move backward

4. Go to step 2.

POSCAT Seminar 1-5714 July 2014

yougatup

1 2

3

4

6

5

7

8Well, how can we implement it ?We have to implement Step 2. and 3.Step 2. is quite simple… but what about Step 3 ?We should move backward.

Page 58: Poscat seminar 9

Graph Traversal

Depth First Search

− Procedure

1. Select a start vertex

2. Move to adjacent vertex we don’t visit yet

3. If there is no such a vertex, move backward

4. Go to step 2.

POSCAT Seminar 1-5814 July 2014

yougatup

1 2

3

4

6

5

7

8Well, how can we implement it ?We have to implement Step 2. and 3.Step 2. is quite simple… but what about Step 3 ?We should move backward.How can we remember the past vertices ?

Page 59: Poscat seminar 9

Graph Traversal

Depth First Search

− Procedure

1. Select a start vertex

2. Move to adjacent vertex we don’t visit yet

3. If there is no such a vertex, move backward

4. Go to step 2.

POSCAT Seminar 1-5914 July 2014

yougatup

1 2

3

4

6

5

7

8Well, how can we implement it ?We have to implement Step 2. and 3.Step 2. is quite simple… but what about Step 3 ?We should move backward.How can we remember the past vertices ? STACK !

Page 60: Poscat seminar 9

Graph Traversal

Depth First Search

− Procedure

1. Select a start vertex

2. Move to adjacent vertex we don’t visit yet

3. If there is no such a vertex, move backward

4. Go to step 2.

POSCAT Seminar 1-6014 July 2014

yougatup

1 2

3

4

6

5

7

8

Page 61: Poscat seminar 9

Graph Traversal

Depth First Search

− Procedure

1. Select a start vertex

2. Move to adjacent vertex we don’t visit yet

3. If there is no such a vertex, move backward

4. Go to step 2.

POSCAT Seminar 1-6114 July 2014

yougatup

1 2

3

4

6

5

7

8

1

Page 62: Poscat seminar 9

Graph Traversal

Depth First Search

− Procedure

1. Select a start vertex

2. Move to adjacent vertex we don’t visit yet

3. If there is no such a vertex, move backward

4. Go to step 2.

POSCAT Seminar 1-6214 July 2014

yougatup

1 2

3

4

6

5

7

8

1

2

Page 63: Poscat seminar 9

Graph Traversal

Depth First Search

− Procedure

1. Select a start vertex

2. Move to adjacent vertex we don’t visit yet

3. If there is no such a vertex, move backward

4. Go to step 2.

POSCAT Seminar 1-6314 July 2014

yougatup

1 2

3

4

6

5

7

8

1

2

3

Page 64: Poscat seminar 9

Graph Traversal

Depth First Search

− Procedure

1. Select a start vertex

2. Move to adjacent vertex we don’t visit yet

3. If there is no such a vertex, move backward

4. Go to step 2.

POSCAT Seminar 1-6414 July 2014

yougatup

1 2

3

4

6

5

7

8

1

2

Simply pop ! Then we get a vertex just before !

Page 65: Poscat seminar 9

Graph Traversal

Depth First Search

− Procedure

1. Select a start vertex

2. Move to adjacent vertex we don’t visit yet

3. If there is no such a vertex, move backward

4. Go to step 2.

POSCAT Seminar 1-6514 July 2014

yougatup

1 2

3

4

6

5

7

8

1

2

4

Page 66: Poscat seminar 9

Graph Traversal

Depth First Search

− Procedure

1. Select a start vertex

2. Move to adjacent vertex we don’t visit yet

3. If there is no such a vertex, move backward

4. Go to step 2.

POSCAT Seminar 1-6614 July 2014

yougatup

1 2

3

4

6

5

7

8

1

2

4

5

Page 67: Poscat seminar 9

Graph Traversal

Depth First Search

− Procedure

1. Select a start vertex

2. Move to adjacent vertex we don’t visit yet

3. If there is no such a vertex, move backward

4. Go to step 2.

POSCAT Seminar 1-6714 July 2014

yougatup

1 2

3

4

6

5

7

8

1

2

4

5

Please do it yourself

Page 68: Poscat seminar 9

Graph Traversal

Breadth First Search

− Use queue to traverse

− Invarient : The current vertex is pointed by front pointer

POSCAT Seminar 1-6814 July 2014

yougatup

1 2

3

4

6

5

7

8

Page 69: Poscat seminar 9

Graph Traversal

Breadth First Search

− Procedure

1. Select a start vertex

2. Push all the adjacent vertices into queue

3. Pop to get a new vertex

4. Go to step 2.

POSCAT Seminar 1-6914 July 2014

yougatup

1 2

3

4

6

5

7

8

front

rear

Page 70: Poscat seminar 9

Graph Traversal

Breadth First Search

− Procedure

1. Select a start vertex

2. Push all the adjacent vertices into queue

3. Pop to get a new vertex

4. Go to step 2.

POSCAT Seminar 1-7014 July 2014

yougatup

1 2

3

4

6

5

7

81

front

rear

Page 71: Poscat seminar 9

Graph Traversal

Breadth First Search

− Procedure

1. Select a start vertex

2. Push all the adjacent vertices into queue

3. Pop to get a new vertex

4. Go to step 2.

POSCAT Seminar 1-7114 July 2014

yougatup

1 2

3

4

6

5

7

81 2 3

front

rear

Page 72: Poscat seminar 9

Graph Traversal

Breadth First Search

− Procedure

1. Select a start vertex

2. Push all the adjacent vertices into queue

3. Pop to get a new vertex

4. Go to step 2.

POSCAT Seminar 1-7214 July 2014

yougatup

1 2

3

4

6

5

7

82 3

front

rear

Page 73: Poscat seminar 9

Graph Traversal

Breadth First Search

− Procedure

1. Select a start vertex

2. Push all the adjacent vertices into queue

3. Pop to get a new vertex

4. Go to step 2.

POSCAT Seminar 1-7314 July 2014

yougatup

1 2

3

4

6

5

7

82 3

front

rear

4

Page 74: Poscat seminar 9

Graph Traversal

Breadth First Search

− Procedure

1. Select a start vertex

2. Push all the adjacent vertices into queue

3. Pop to get a new vertex

4. Go to step 2.

POSCAT Seminar 1-7414 July 2014

yougatup

1 2

3

4

6

5

7

83

front

rear

4

Page 75: Poscat seminar 9

Graph Traversal

Breadth First Search

− Procedure

1. Select a start vertex

2. Push all the adjacent vertices into queue

3. Pop to get a new vertex

4. Go to step 2.

POSCAT Seminar 1-7514 July 2014

yougatup

1 2

3

4

6

5

7

8

front

rear

4

Page 76: Poscat seminar 9

Graph Traversal

Breadth First Search

− Procedure

1. Select a start vertex

2. Push all the adjacent vertices into queue

3. Pop to get a new vertex

4. Go to step 2.

POSCAT Seminar 1-7614 July 2014

yougatup

1 2

3

4

6

5

7

8

front

rear

4 5 6

Page 77: Poscat seminar 9

Graph Traversal

Breadth First Search

− Procedure

1. Select a start vertex

2. Push all the adjacent vertices into queue

3. Pop to get a new vertex

4. Go to step 2.

POSCAT Seminar 1-7714 July 2014

yougatup

1 2

3

4

6

5

7

8

front

rear

5 6

Page 78: Poscat seminar 9

Graph Traversal

Breadth First Search

− Procedure

1. Select a start vertex

2. Push all the adjacent vertices into queue

3. Pop to get a new vertex

4. Go to step 2.

POSCAT Seminar 1-7814 July 2014

yougatup

1 2

3

4

6

5

7

8

front

rear

5 6 7

Page 79: Poscat seminar 9

Graph Traversal

Breadth First Search

− Procedure

1. Select a start vertex

2. Push all the adjacent vertices into queue

3. Pop to get a new vertex

4. Go to step 2.

POSCAT Seminar 1-7914 July 2014

yougatup

1 2

3

4

6

5

7

8

front

rear

6 7

Page 80: Poscat seminar 9

Graph Traversal

Breadth First Search

− Procedure

1. Select a start vertex

2. Push all the adjacent vertices into queue

3. Pop to get a new vertex

4. Go to step 2.

POSCAT Seminar 1-8014 July 2014

yougatup

1 2

3

4

6

5

7

8

front

rear

7

Page 81: Poscat seminar 9

Graph Traversal

Breadth First Search

− Procedure

1. Select a start vertex

2. Push all the adjacent vertices into queue

3. Pop to get a new vertex

4. Go to step 2.

POSCAT Seminar 1-8114 July 2014

yougatup

1 2

3

4

6

5

7

8

front

rear

7 8

Page 82: Poscat seminar 9

Graph Traversal

Breadth First Search

− Procedure

1. Select a start vertex

2. Push all the adjacent vertices into queue

3. Pop to get a new vertex

4. Go to step 2.

POSCAT Seminar 1-8214 July 2014

yougatup

1 2

3

4

6

5

7

8

front

rear

8

Page 83: Poscat seminar 9

Graph Traversal

Breadth First Search

− Procedure

1. Select a start vertex

2. Push all the adjacent vertices into queue

3. Pop to get a new vertex

4. Go to step 2.

POSCAT Seminar 1-8314 July 2014

yougatup

1 2

3

4

6

5

7

8

front

rear

Page 84: Poscat seminar 9

Graph Traversal

Breadth First Search

− Procedure

1. Select a start vertex

2. Push all the adjacent vertices into queue

3. Pop to get a new vertex

4. Go to step 2.

POSCAT Seminar 1-8414 July 2014

yougatup

1 2

3

4

6

5

7

8

front

rear

Done. Queue is empty.

Page 85: Poscat seminar 9

Graph Traversal

DFS vs BFS

− We have to choose what search technique to use

− Example ?

POSCAT Seminar 1-8514 July 2014

yougatup

1 2

3

4

6

5

7

8

Page 86: Poscat seminar 9

Flood Fill

Problem

Given a map, categorize each cell as inner or outer cell

Suppose that the outer-most cell is always 0

POSCAT Seminar 1-8614 July 2014

yougatup

0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0

0 0 1 1 1 1 0 0 0 0

0 0 1 0 0 0 1 0 0 0

0 0 1 0 0 0 0 1 0 0

0 0 1 0 0 0 0 0 1 0

0 0 1 0 0 0 0 0 1 0

0 0 0 1 0 1 0 0 1 0

0 0 0 0 1 0 1 1 1 0

0 0 0 0 0 0 0 0 0 0

Page 87: Poscat seminar 9

Flood Fill

Problem

Given a map, categorize each cell as inner or outer cell

Suppose that the outer-most cell is always 0

POSCAT Seminar 1-8714 July 2014

yougatup

0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0

0 0 1 1 1 1 0 0 0 0

0 0 1 0 0 0 1 0 0 0

0 0 1 0 0 0 0 1 0 0

0 0 1 0 0 0 0 0 1 0

0 0 1 0 0 0 0 0 1 0

0 0 0 1 0 1 0 0 1 0

0 0 0 0 1 0 1 1 1 0

0 0 0 0 0 0 0 0 0 0

Page 88: Poscat seminar 9

Flood Fill

Problem

Any idea ?

POSCAT Seminar 1-8814 July 2014

yougatup

0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0

0 0 1 1 1 1 0 0 0 0

0 0 1 0 0 0 1 0 0 0

0 0 1 0 0 0 0 1 0 0

0 0 1 0 0 0 0 0 1 0

0 0 1 0 0 0 0 0 1 0

0 0 0 1 0 1 0 0 1 0

0 0 0 0 1 0 1 1 1 0

0 0 0 0 0 0 0 0 0 0

Page 89: Poscat seminar 9

Flood Fill

Problem

Any idea ? Modeling it as a graph !

POSCAT Seminar 1-8914 July 2014

yougatup

0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0

0 0 1 1 1 1 0 0 0 0

0 0 1 0 0 0 1 0 0 0

0 0 1 0 0 0 0 1 0 0

0 0 1 0 0 0 0 0 1 0

0 0 1 0 0 0 0 0 1 0

0 0 0 1 0 1 0 0 1 0

0 0 0 0 1 0 1 1 1 0

0 0 0 0 0 0 0 0 0 0

Page 90: Poscat seminar 9

Flood Fill

Problem

Any idea ? Modeling it as a graph !

Each cell is considered as a vertex. How about edge ?

POSCAT Seminar 1-9014 July 2014

yougatup

0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0

0 0 1 1 1 1 0 0 0 0

0 0 1 0 0 0 1 0 0 0

0 0 1 0 0 0 0 1 0 0

0 0 1 0 0 0 0 0 1 0

0 0 1 0 0 0 0 0 1 0

0 0 0 1 0 1 0 0 1 0

0 0 0 0 1 0 1 1 1 0

0 0 0 0 0 0 0 0 0 0

Page 91: Poscat seminar 9

Flood Fill

Problem

Any idea ? Modeling it as a graph !

there is a edge if two vertices are adjacent in the map

POSCAT Seminar 1-9114 July 2014

yougatup

0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0

0 0 1 1 1 1 0 0 0 0

0 0 1 0 0 0 1 0 0 0

0 0 1 0 0 0 0 1 0 0

0 0 1 0 0 0 0 0 1 0

0 0 1 0 0 0 0 0 1 0

0 0 0 1 0 1 0 0 1 0

0 0 0 0 1 0 1 1 1 0

0 0 0 0 0 0 0 0 0 0

Page 92: Poscat seminar 9

Flood Fill

Problem

Perform DFS or BFS starting from the outermost cell !

However, we never move to the ‘1’ cell

POSCAT Seminar 1-9214 July 2014

yougatup

0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0

0 0 1 1 1 1 0 0 0 0

0 0 1 0 0 0 1 0 0 0

0 0 1 0 0 0 0 1 0 0

0 0 1 0 0 0 0 0 1 0

0 0 1 0 0 0 0 0 1 0

0 0 0 1 0 1 0 0 1 0

0 0 0 0 1 0 1 1 1 0

0 0 0 0 0 0 0 0 0 0

Page 93: Poscat seminar 9

Flood Fill

Problem

BFS ?

POSCAT Seminar 1-9314 July 2014

yougatup

0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0

0 0 1 1 1 1 0 0 0 0

0 0 1 0 0 0 1 0 0 0

0 0 1 0 0 0 0 1 0 0

0 0 1 0 0 0 0 0 1 0

0 0 1 0 0 0 0 0 1 0

0 0 0 1 0 1 0 0 1 0

0 0 0 0 1 0 1 1 1 0

0 0 0 0 0 0 0 0 0 0

Page 94: Poscat seminar 9

Flood Fill

Problem

BFS ?

POSCAT Seminar 1-9414 July 2014

yougatup

0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0

0 0 1 1 1 1 0 0 0 0

0 0 1 0 0 0 1 0 0 0

0 0 1 0 0 0 0 1 0 0

0 0 1 0 0 0 0 0 1 0

0 0 1 0 0 0 0 0 1 0

0 0 0 1 0 1 0 0 1 0

0 0 0 0 1 0 1 1 1 0

0 0 0 0 0 0 0 0 0 0

Page 95: Poscat seminar 9

Flood Fill

Problem

BFS ?

POSCAT Seminar 1-9514 July 2014

yougatup

0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0

0 0 1 1 1 1 0 0 0 0

0 0 1 0 0 0 1 0 0 0

0 0 1 0 0 0 0 1 0 0

0 0 1 0 0 0 0 0 1 0

0 0 1 0 0 0 0 0 1 0

0 0 0 1 0 1 0 0 1 0

0 0 0 0 1 0 1 1 1 0

0 0 0 0 0 0 0 0 0 0

Page 96: Poscat seminar 9

Flood Fill

Problem

BFS ?

POSCAT Seminar 1-9614 July 2014

yougatup

0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0

0 0 1 1 1 1 0 0 0 0

0 0 1 0 0 0 1 0 0 0

0 0 1 0 0 0 0 1 0 0

0 0 1 0 0 0 0 0 1 0

0 0 1 0 0 0 0 0 1 0

0 0 0 1 0 1 0 0 1 0

0 0 0 0 1 0 1 1 1 0

0 0 0 0 0 0 0 0 0 0

Page 97: Poscat seminar 9

Flood Fill

Problem

BFS ?

POSCAT Seminar 1-9714 July 2014

yougatup

0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0

0 0 1 1 1 1 0 0 0 0

0 0 1 0 0 0 1 0 0 0

0 0 1 0 0 0 0 1 0 0

0 0 1 0 0 0 0 0 1 0

0 0 1 0 0 0 0 0 1 0

0 0 0 1 0 1 0 0 1 0

0 0 0 0 1 0 1 1 1 0

0 0 0 0 0 0 0 0 0 0

Page 98: Poscat seminar 9

Flood Fill

Problem

BFS ?

POSCAT Seminar 1-9814 July 2014

yougatup

0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0

0 0 1 1 1 1 0 0 0 0

0 0 1 0 0 0 1 0 0 0

0 0 1 0 0 0 0 1 0 0

0 0 1 0 0 0 0 0 1 0

0 0 1 0 0 0 0 0 1 0

0 0 0 1 0 1 0 0 1 0

0 0 0 0 1 0 1 1 1 0

0 0 0 0 0 0 0 0 0 0

Page 99: Poscat seminar 9

Flood Fill

Problem

BFS ?

POSCAT Seminar 1-9914 July 2014

yougatup

0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0

0 0 1 1 1 1 0 0 0 0

0 0 1 0 0 0 1 0 0 0

0 0 1 0 0 0 0 1 0 0

0 0 1 0 0 0 0 0 1 0

0 0 1 0 0 0 0 0 1 0

0 0 0 1 0 1 0 0 1 0

0 0 0 0 1 0 1 1 1 0

0 0 0 0 0 0 0 0 0 0

Page 100: Poscat seminar 9

Flood Fill

Problem

BFS ?

POSCAT Seminar 1-10014 July 2014

yougatup

0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0

0 0 1 1 1 1 0 0 0 0

0 0 1 0 0 0 1 0 0 0

0 0 1 0 0 0 0 1 0 0

0 0 1 0 0 0 0 0 1 0

0 0 1 0 0 0 0 0 1 0

0 0 0 1 0 1 0 0 1 0

0 0 0 0 1 0 1 1 1 0

0 0 0 0 0 0 0 0 0 0

Page 101: Poscat seminar 9

Flood Fill

Problem

BFS ?

POSCAT Seminar 1-10114 July 2014

yougatup

0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0

0 0 1 1 1 1 0 0 0 0

0 0 1 0 0 0 1 0 0 0

0 0 1 0 0 0 0 1 0 0

0 0 1 0 0 0 0 0 1 0

0 0 1 0 0 0 0 0 1 0

0 0 0 1 0 1 0 0 1 0

0 0 0 0 1 0 1 1 1 0

0 0 0 0 0 0 0 0 0 0

Page 102: Poscat seminar 9

Flood Fill

Problem

BFS ?

POSCAT Seminar 1-10214 July 2014

yougatup

0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0

0 0 1 1 1 1 0 0 0 0

0 0 1 0 0 0 1 0 0 0

0 0 1 0 0 0 0 1 0 0

0 0 1 0 0 0 0 0 1 0

0 0 1 0 0 0 0 0 1 0

0 0 0 1 0 1 0 0 1 0

0 0 0 0 1 0 1 1 1 0

0 0 0 0 0 0 0 0 0 0

Page 103: Poscat seminar 9

Flood Fill

Problem

BFS ?

POSCAT Seminar 1-10314 July 2014

yougatup

0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0

0 0 1 1 1 1 0 0 0 0

0 0 1 0 0 0 1 0 0 0

0 0 1 0 0 0 0 1 0 0

0 0 1 0 0 0 0 0 1 0

0 0 1 0 0 0 0 0 1 0

0 0 0 1 0 1 0 0 1 0

0 0 0 0 1 0 1 1 1 0

0 0 0 0 0 0 0 0 0 0

Page 104: Poscat seminar 9

Flood Fill

Problem

BFS ?

POSCAT Seminar 1-10414 July 2014

yougatup

0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0

0 0 1 1 1 1 0 0 0 0

0 0 1 0 0 0 1 0 0 0

0 0 1 0 0 0 0 1 0 0

0 0 1 0 0 0 0 0 1 0

0 0 1 0 0 0 0 0 1 0

0 0 0 1 0 1 0 0 1 0

0 0 0 0 1 0 1 1 1 0

0 0 0 0 0 0 0 0 0 0

Page 105: Poscat seminar 9

Flood Fill

Problem

Can we apply this flood fill algorithm to solve a problem ?

POSCAT Seminar 1-10514 July 2014

yougatup

0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0

0 0 1 1 1 1 0 0 0 0

0 0 1 0 0 0 1 0 0 0

0 0 1 0 0 0 0 1 0 0

0 0 1 0 0 0 0 0 1 0

0 0 1 0 0 0 0 0 1 0

0 0 0 1 0 1 0 0 1 0

0 0 0 0 1 0 1 1 1 0

0 0 0 0 0 0 0 0 0 0

Page 106: Poscat seminar 9

Flood Fill

Problem

Find the minimum length from the start cell and the end cell

POSCAT Seminar 1-10614 July 2014

yougatup

0 0 0 0 0 0 0 0 0 0

1 1 0 0 0 1 0 0 0 0

0 1 1 1 1 0 0 0 1 0

0 1 1 0 0 0 1 0 1 0

0 1 1 0 0 0 1 1 0 0

0 1 1 0 1 0 0 0 1 0

0 1 1 0 1 0 1 0 1 0

0 1 0 0 1 0 1 0 0 0

0 1 1 0 1 0 1 1 1 0

0 0 0 0 0 0 0 1 0 0

Page 107: Poscat seminar 9

Flood Fill

Problem

Find the minimum length from the start cell and the end cell

Here !

POSCAT Seminar 1-10714 July 2014

yougatup

0 0 0 0 0 0 0 0 0 0

1 1 0 0 0 1 0 0 0 0

0 1 1 1 1 0 0 0 1 0

0 1 1 0 0 0 1 0 1 0

0 1 1 0 0 0 1 1 0 0

0 1 1 0 1 0 0 0 1 0

0 1 1 0 1 0 1 0 1 0

0 1 0 0 1 0 1 0 0 0

0 1 1 0 1 0 1 1 1 0

0 0 0 0 0 0 0 1 0 0

Page 108: Poscat seminar 9

Flood Fill

Problem

Find the minimum length from the start cell and the end cell

However, there is another path which is longer !

POSCAT Seminar 1-10814 July 2014

yougatup

0 0 0 0 0 0 0 0 0 0

1 1 0 0 0 1 0 0 0 0

0 1 1 1 1 0 0 0 1 0

0 1 1 0 0 0 1 0 1 0

0 1 1 0 0 0 1 1 0 0

0 1 1 0 1 0 0 0 1 0

0 1 1 0 1 0 1 0 1 0

0 1 0 0 1 0 1 0 0 0

0 1 1 0 1 0 1 1 1 0

0 0 0 0 0 0 0 1 0 0

Page 109: Poscat seminar 9

Flood Fill

Problem

How can we find the optimal path ?

POSCAT Seminar 1-10914 July 2014

yougatup

0 0 0 0 0 0 0 0 0 0

1 1 0 0 0 1 0 0 0 0

0 1 1 1 1 0 0 0 1 0

0 1 1 0 0 0 1 0 1 0

0 1 1 0 0 0 1 1 0 0

0 1 1 0 1 0 0 0 1 0

0 1 1 0 1 0 1 0 1 0

0 1 0 0 1 0 1 0 0 0

0 1 1 0 1 0 1 1 1 0

0 0 0 0 0 0 0 1 0 0

Page 110: Poscat seminar 9

Flood Fill

Problem

How can we find the optimal path ?

Let 𝑃 𝑖, 𝑗 = 𝑡ℎ𝑒 𝑠ℎ𝑜𝑟𝑡𝑒𝑠𝑡 𝑙𝑒𝑛𝑔𝑡ℎ 𝑡𝑜 (𝑖, 𝑗), and fill it by using flood fill

POSCAT Seminar 1-11014 July 2014

yougatup

0 0 0 0 0 0 0 0 0 0

1 1 0 0 0 1 0 0 0 0

0 1 1 1 1 0 0 0 1 0

0 1 1 0 0 0 1 0 1 0

0 1 1 0 0 0 1 1 0 0

0 1 1 0 1 0 0 0 1 0

0 1 1 0 1 0 1 0 1 0

0 1 0 0 1 0 1 0 0 0

0 1 1 0 1 0 1 1 1 0

0 0 0 0 0 0 0 1 0 0

Page 111: Poscat seminar 9

Flood Fill

Problem

Which traversal is better? DFS or BFS ?

POSCAT Seminar 1-11114 July 2014

yougatup

0 0 0 0 0 0 0 0 0 0

1 1 0 0 0 1 0 0 0 0

0 1 1 1 1 0 0 0 1 0

0 1 1 0 0 0 1 0 1 0

0 1 1 0 0 0 1 1 0 0

0 1 1 0 1 0 0 0 1 0

0 1 1 0 1 0 1 0 1 0

0 1 0 0 1 0 1 0 0 0

0 1 1 0 1 0 1 1 1 0

0 0 0 0 0 0 0 1 0 0

Page 112: Poscat seminar 9

Flood Fill

Problem

Which traversal is better? DFS or BFS ?

Definitely, BFS is better in this time. Think about the reason

POSCAT Seminar 1-11214 July 2014

yougatup

0 0 0 0 0 0 0 0 0 0

1 1 0 0 0 1 0 0 0 0

0 1 1 1 1 0 0 0 1 0

0 1 1 0 0 0 1 0 1 0

0 1 1 0 0 0 1 1 0 0

0 1 1 0 1 0 0 0 1 0

0 1 1 0 1 0 1 0 1 0

0 1 0 0 1 0 1 0 0 0

0 1 1 0 1 0 1 1 1 0

0 0 0 0 0 0 0 1 0 0

Page 113: Poscat seminar 9

Flood Fill

Problem

How can we find the optimal path ?

Let 𝑃 𝑖, 𝑗 = 𝑡ℎ𝑒 𝑠ℎ𝑜𝑟𝑡𝑒𝑠𝑡 𝑙𝑒𝑛𝑔𝑡ℎ 𝑡𝑜 (𝑖, 𝑗), and fill it by using flood fill

POSCAT Seminar 1-11314 July 2014

yougatup

0 0 0 0 0 0 0 0 0 0

1 1 0 0 0 1 0 0 0 0

0 1 1 1 1 0 0 0 1 0

0 1 1 0 0 0 1 0 1 0

0 1 1 0 0 0 1 1 0 0

0 1 1 0 1 0 0 0 1 0

0 1 1 0 1 0 1 0 1 0

0 1 0 0 1 0 1 0 0 0

0 1 1 0 1 0 1 1 1 0

0 0 0 0 0 0 0 1 0 0

Page 114: Poscat seminar 9

Flood Fill

Problem

How can we find the optimal path ?

Let 𝑃 𝑖, 𝑗 = 𝑡ℎ𝑒 𝑠ℎ𝑜𝑟𝑡𝑒𝑠𝑡 𝑙𝑒𝑛𝑔𝑡ℎ 𝑡𝑜 (𝑖, 𝑗), and fill it by using flood fill

POSCAT Seminar 1-11414 July 2014

yougatup

0 0 0 0 0 0 0 0 0 0

1 1 0 0 0 1 0 0 0 0

0 1 1 1 1 0 0 0 1 0

0 1 1 0 0 0 1 0 1 0

0 1 1 0 0 0 1 1 0 0

0 1 1 0 1 0 0 0 1 0

0 1 1 0 1 0 1 0 1 0

0 1 0 0 1 0 1 0 0 0

1 1 1 0 1 0 1 1 1 0

0 1 0 0 0 0 0 1 0 0

Page 115: Poscat seminar 9

Flood Fill

Problem

How can we find the optimal path ?

Let 𝑃 𝑖, 𝑗 = 𝑡ℎ𝑒 𝑠ℎ𝑜𝑟𝑡𝑒𝑠𝑡 𝑙𝑒𝑛𝑔𝑡ℎ 𝑡𝑜 (𝑖, 𝑗), and fill it by using flood fill

POSCAT Seminar 1-11514 July 2014

yougatup

0 0 0 0 0 0 0 0 0 0

1 1 0 0 0 1 0 0 0 0

0 1 1 1 1 0 0 0 1 0

0 1 1 0 0 0 1 0 1 0

0 1 1 0 0 0 1 1 0 0

0 1 1 0 1 0 0 0 1 0

0 1 1 0 1 0 1 0 1 0

2 1 0 0 1 0 1 0 0 0

1 1 1 0 1 0 1 1 1 0

0 1 2 0 0 0 0 1 0 0

Page 116: Poscat seminar 9

Flood Fill

Problem

How can we find the optimal path ?

Let 𝑃 𝑖, 𝑗 = 𝑡ℎ𝑒 𝑠ℎ𝑜𝑟𝑡𝑒𝑠𝑡 𝑙𝑒𝑛𝑔𝑡ℎ 𝑡𝑜 (𝑖, 𝑗), and fill it by using flood fill

POSCAT Seminar 1-11614 July 2014

yougatup

0 0 0 0 0 0 0 0 0 0

1 1 0 0 0 1 0 0 0 0

0 1 1 1 1 0 0 0 1 0

0 1 1 0 0 0 1 0 1 0

0 1 1 0 0 0 1 1 0 0

0 1 1 0 1 0 0 0 1 0

3 1 1 0 1 0 1 0 1 0

2 1 0 0 1 0 1 0 0 0

1 1 1 0 1 0 1 1 1 0

0 1 2 3 0 0 0 1 0 0

Page 117: Poscat seminar 9

Flood Fill

Problem

How can we find the optimal path ?

Let 𝑃 𝑖, 𝑗 = 𝑡ℎ𝑒 𝑠ℎ𝑜𝑟𝑡𝑒𝑠𝑡 𝑙𝑒𝑛𝑔𝑡ℎ 𝑡𝑜 (𝑖, 𝑗), and fill it by using flood fill

POSCAT Seminar 1-11714 July 2014

yougatup

0 0 0 0 0 0 0 0 0 0

1 1 0 0 0 1 0 0 0 0

0 1 1 1 1 0 0 0 1 0

0 1 1 0 0 0 1 0 1 0

0 1 1 0 0 0 1 1 0 0

4 1 1 0 1 0 0 0 1 0

3 1 1 0 1 0 1 0 1 0

2 1 0 0 1 0 1 0 0 0

1 1 1 4 1 0 1 1 1 0

0 1 2 3 4 0 0 1 0 0

Page 118: Poscat seminar 9

Flood Fill

Problem

How can we find the optimal path ?

Let 𝑃 𝑖, 𝑗 = 𝑡ℎ𝑒 𝑠ℎ𝑜𝑟𝑡𝑒𝑠𝑡 𝑙𝑒𝑛𝑔𝑡ℎ 𝑡𝑜 (𝑖, 𝑗), and fill it by using flood fill

POSCAT Seminar 1-11814 July 2014

yougatup

0 0 0 0 0 0 0 0 0 0

1 1 0 0 0 1 0 0 0 0

0 1 1 1 1 0 0 0 1 0

0 1 1 0 0 0 1 0 1 0

5 1 1 0 0 0 1 1 0 0

4 1 1 0 1 0 0 0 1 0

3 1 1 0 1 0 1 0 1 0

2 1 0 5 1 0 1 0 0 0

1 1 1 4 1 0 1 1 1 0

0 1 2 3 4 5 0 1 0 0

Page 119: Poscat seminar 9

Flood Fill

Problem

How can we find the optimal path ?

Let 𝑃 𝑖, 𝑗 = 𝑡ℎ𝑒 𝑠ℎ𝑜𝑟𝑡𝑒𝑠𝑡 𝑙𝑒𝑛𝑔𝑡ℎ 𝑡𝑜 (𝑖, 𝑗), and fill it by using flood fill

POSCAT Seminar 1-11914 July 2014

yougatup

0 0 0 0 0 0 0 0 0 0

1 1 0 0 0 1 0 0 0 0

0 1 1 1 1 0 0 0 1 0

6 1 1 0 0 0 1 0 1 0

5 1 1 0 0 0 1 1 0 0

4 1 1 0 1 0 0 0 1 0

3 1 1 6 1 0 1 0 1 0

2 1 6 5 1 0 1 0 0 0

1 1 1 4 1 6 1 1 1 0

0 1 2 3 4 5 6 1 0 0

Page 120: Poscat seminar 9

Flood Fill

Problem

How can we find the optimal path ?

Let 𝑃 𝑖, 𝑗 = 𝑡ℎ𝑒 𝑠ℎ𝑜𝑟𝑡𝑒𝑠𝑡 𝑙𝑒𝑛𝑔𝑡ℎ 𝑡𝑜 (𝑖, 𝑗), and fill it by using flood fill

POSCAT Seminar 1-12014 July 2014

yougatup

0 0 0 0 0 0 0 0 0 0

1 1 0 0 0 1 0 0 0 0

7 1 1 1 1 0 0 0 1 0

6 1 1 0 0 0 1 0 1 0

5 1 1 8 0 0 1 1 0 0

4 1 1 7 1 0 0 0 1 0

3 1 1 6 1 8 1 0 1 0

2 1 6 5 1 7 1 0 0 0

1 1 1 4 1 6 1 1 1 0

0 1 2 3 4 5 6 1 0 0

Page 121: Poscat seminar 9

Flood Fill

Problem

How can we find the optimal path ?

Let 𝑃 𝑖, 𝑗 = 𝑡ℎ𝑒 𝑠ℎ𝑜𝑟𝑡𝑒𝑠𝑡 𝑙𝑒𝑛𝑔𝑡ℎ 𝑡𝑜 (𝑖, 𝑗), and fill it by using flood fill

POSCAT Seminar 1-12114 July 2014

yougatup

0 0 0 0 0 0 0 0 0 0

1 1 0 0 0 1 0 0 0 0

7 1 1 1 1 0 0 0 1 0

6 1 1 9 0 0 1 0 1 0

5 1 1 8 9 0 1 1 0 0

4 1 1 7 1 9 0 0 1 0

3 1 1 6 1 8 1 0 1 0

2 1 6 5 1 7 1 0 0 0

1 1 1 4 1 6 1 1 1 0

0 1 2 3 4 5 6 1 0 0

Fill it yourself

Page 122: Poscat seminar 9

Connected Component

Problem

Given a graph, determine whether this graph is connected or not

POSCAT Seminar 1-12214 July 2014

yougatup

1 2

3

4

6

5

7

8

Page 123: Poscat seminar 9

Connected Component

Problem

Given a graph, determine whether this graph is connected or not

POSCAT Seminar 1-12314 July 2014

yougatup

1 2

3

4

6

5

7

8

Is it connected ?

Page 124: Poscat seminar 9

Connected Component

Problem

Given a graph, determine whether this graph is connected or not

POSCAT Seminar 1-12414 July 2014

yougatup

1 2

3

4

6

5

7

8

Is it connected ? NO!

Page 125: Poscat seminar 9

Connected Component

Problem

Given a graph, determine whether this graph is connected or not

One traversal from any start vertex is enough. Easy.

POSCAT Seminar 1-12514 July 2014

yougatup

1 2

3

4

6

5

7

8

Page 126: Poscat seminar 9

Connected Component

Problem

Given a graph, determine whether this graph is connected or not

One traversal from any start vertex is enough. Easy.

What if this graph is directed graph ?

POSCAT Seminar 1-12614 July 2014

yougatup

1 2

3

4

6

5

7

8

Page 127: Poscat seminar 9

Connected Component

Problem

Given a graph, determine whether this graph is connected or not

One traversal from any start vertex is enough. Easy.

What if this graph is directed graph ?

POSCAT Seminar 1-12714 July 2014

yougatup

1 2

3

4

6

5

7

8

Page 128: Poscat seminar 9

Strongly Connected Component

Problem

Given a direct graph, Find Strongly Connected Component

SCC : a graph is strongly connected if every vertex is reachable from every other vertex

POSCAT Seminar 1-12814 July 2014

yougatup

1 2

3

4

6

5

7

8

Page 129: Poscat seminar 9

Strongly Connected Component

Problem

Given a direct graph, Find Strongly Connected Component

SCC : a graph is strongly connected if every vertex is reachable from every other vertex

POSCAT Seminar 1-12914 July 2014

yougatup

1 2

3

4

6

5

7

8

Page 130: Poscat seminar 9

Strongly Connected Component

Problem

Suppose that there is strongly connected component. ( just imagine )

POSCAT Seminar 1-13014 July 2014

yougatup

Page 131: Poscat seminar 9

Strongly Connected Component

Problem

Suppose that there is strongly connected component. ( just imagine )

Then we can make a graph consisting of “big” vertex

( big vertex makes a strongly connected component )

Let me call this graph as a meta graph

POSCAT Seminar 1-13114 July 2014

yougatup

Page 132: Poscat seminar 9

Strongly Connected Component

Problem

Trivially, this meta graph has no cycle.

If not, they had to shrink into a vertex

POSCAT Seminar 1-13214 July 2014

yougatup

Page 133: Poscat seminar 9

Strongly Connected Component

Problem

Trivially, this meta graph has no cycle.

If not, they had to shrink into a vertex

POSCAT Seminar 1-13314 July 2014

yougatup

Page 134: Poscat seminar 9

Strongly Connected Component

Problem

Trivially, this meta graph has no cycle.

If not, they had to shrink into a vertex

POSCAT Seminar 1-13414 July 2014

yougatup

Page 135: Poscat seminar 9

Strongly Connected Component

Problem

Think about the leaf big node. (a big node which has no outer edge)

Choose one vertex from the leaf big node.

POSCAT Seminar 1-13514 July 2014

yougatup

Page 136: Poscat seminar 9

Strongly Connected Component

Problem

If we perform traverse started from the vertex, then it will traverse

whole big node because the big node is SCC

If not, contradiction.

POSCAT Seminar 1-13614 July 2014

yougatup

We can find all the nodes

Page 137: Poscat seminar 9

Strongly Connected Component

Problem

Therefore, we can derive a beautiful algorithm !

1. Find a vertex within the leaf big node

2. Traverse started from the vertex we get a SCC

3. Remove that big node

4. Repeat it

POSCAT Seminar 1-13714 July 2014

yougatup

Page 138: Poscat seminar 9

Strongly Connected Component

Problem

Therefore, we can derive a beautiful algorithm !

1. Find a vertex within the leaf big node

2. Traverse started from the vertex

3. Remove that big node

4. Repeat it

POSCAT Seminar 1-13814 July 2014

yougatup

Page 139: Poscat seminar 9

Strongly Connected Component

Problem

Therefore, we can derive a beautiful algorithm !

1. Find a vertex within the leaf big node

2. Traverse started from the vertex

3. Remove that big node

4. Repeat it

POSCAT Seminar 1-13914 July 2014

yougatup

Page 140: Poscat seminar 9

Strongly Connected Component

Problem

Therefore, we can derive a beautiful algorithm !

1. Find a vertex within the leaf big node

2. Traverse started from the vertex

3. Remove that big node

4. Repeat it

POSCAT Seminar 1-14014 July 2014

yougatup

Page 141: Poscat seminar 9

Strongly Connected Component

Problem

Therefore, we can derive a beautiful algorithm !

1. Find a vertex within the leaf big node

2. Traverse started from the vertex

3. Remove that big node

4. Repeat it

POSCAT Seminar 1-14114 July 2014

yougatup

Page 142: Poscat seminar 9

Strongly Connected Component

Problem

Therefore, we can derive a beautiful algorithm !

1. Find a vertex within the leaf big node

2. Traverse started from the vertex

3. Remove that big node

4. Repeat it

POSCAT Seminar 1-14214 July 2014

yougatup

Page 143: Poscat seminar 9

Strongly Connected Component

Problem

Therefore, we can derive a beautiful algorithm !

1. Find a vertex within the leaf big node

2. Traverse started from the vertex

3. Remove that big node

4. Repeat it

POSCAT Seminar 1-14314 July 2014

yougatup

Page 144: Poscat seminar 9

Strongly Connected Component

Problem

Therefore, we can derive a beautiful algorithm !

1. Find a vertex within the leaf big node

2. Traverse started from the vertex

3. Remove that big node

4. Repeat it

POSCAT Seminar 1-14414 July 2014

yougatup

Page 145: Poscat seminar 9

Strongly Connected Component

Problem

Therefore, we can derive a beautiful algorithm !

1. Find a vertex within the leaf big node

2. Traverse started from the vertex

3. Remove that big node

4. Repeat it

POSCAT Seminar 1-14514 July 2014

yougatup

Page 146: Poscat seminar 9

Strongly Connected Component

Problem

Therefore, we can derive a beautiful algorithm !

1. Find a vertex within the leaf big node

2. Traverse started from the vertex

3. Remove that big node

4. Repeat it

POSCAT Seminar 1-14614 July 2014

yougatup

Page 147: Poscat seminar 9

Strongly Connected Component

Problem

Therefore, we can derive a beautiful algorithm !

1. Find a vertex within the leaf big node

2. Traverse started from the vertex

3. Remove that big node

4. Repeat it

POSCAT Seminar 1-14714 July 2014

yougatup

Page 148: Poscat seminar 9

Strongly Connected Component

Problem

Therefore, we can derive a beautiful algorithm !

1. Find a vertex within the leaf big node

2. Traverse started from the vertex

3. Remove that big node

4. Repeat it

Is it correct ?

POSCAT Seminar 1-14814 July 2014

yougatup

Page 149: Poscat seminar 9

Strongly Connected Component

Problem

Therefore, we can derive a beautiful algorithm !

1. Find a vertex within the leaf big node

2. Traverse started from the vertex

3. Remove that big node

4. Repeat it

Is it correct ? Sure

POSCAT Seminar 1-14914 July 2014

yougatup

Page 150: Poscat seminar 9

Strongly Connected Component

Problem

Therefore, we can derive a beautiful algorithm !

1. Find a vertex within the leaf big node

2. Traverse started from the vertex

3. Remove that big node

4. Repeat it

However, how can we find a vertex in step 1 ?

We don’t know whether a vertex is contained in the leaf big node or not

Actually, it is impossible

POSCAT Seminar 1-15014 July 2014

yougatup

Page 151: Poscat seminar 9

Strongly Connected Component

Problem

OTL …

POSCAT Seminar 1-15114 July 2014

yougatup

Page 152: Poscat seminar 9

Strongly Connected Component

Problem

OTL …

We can choose a vertex, but we don’t know whether it is contained in

the leaf big node

POSCAT Seminar 1-15214 July 2014

yougatup

Page 153: Poscat seminar 9

Strongly Connected Component

Problem

OTL …

Then, just traverse it ! Then it will traverse all the big node reached

from the vertex

POSCAT Seminar 1-15314 July 2014

yougatup

Page 154: Poscat seminar 9

Strongly Connected Component

Problem

OTL …

And reverse the whole graph !

POSCAT Seminar 1-15414 July 2014

yougatup

Page 155: Poscat seminar 9

Strongly Connected Component

Problem

OTL …

And reverse the whole graph !

Then our start vertex becomes the vertex contained in the leaf node !

POSCAT Seminar 1-15514 July 2014

yougatup

We don’t know its existencebecause we don’t traverse it

Page 156: Poscat seminar 9

Strongly Connected Component

Problem

OTL …

And reverse the whole graph !

Then our start vertex becomes the vertex contained in the leaf node !

It is strong evidence that we can perform our algorithm

POSCAT Seminar 1-15614 July 2014

yougatup

We don’t know its existencebecause we don’t traverse it

Page 157: Poscat seminar 9

Strongly Connected Component

Overall procedure

1. Choose a start vertex

2. Perform DFS, and write the exit time for all the vertices

3. After doing DFS, reverse whole graph

4. Perform DFS or BFS starting from a vertex whose exit time is the

largest

5. All the vertices gathering in step 4 performs a SCC

POSCAT Seminar 1-15714 July 2014

yougatup

Page 158: Poscat seminar 9

Strongly Connected Component

Overall procedure

POSCAT Seminar 1-15814 July 2014

yougatup

1 2

3

4

6

5

7

8

Page 159: Poscat seminar 9

Strongly Connected Component

Overall procedure

Pick a start vertex

POSCAT Seminar 1-15914 July 2014

yougatup

1 2

3

4

6

5

7

8

Page 160: Poscat seminar 9

Strongly Connected Component

Overall procedure

Pick a start vertex

Perform DFS and write the exit time

POSCAT Seminar 1-16014 July 2014

yougatup

1 2

3

4

6

5

7

8

Page 161: Poscat seminar 9

Strongly Connected Component

Overall procedure

Pick a start vertex

Perform DFS and write the exit time

POSCAT Seminar 1-16114 July 2014

yougatup

1 2

3

4

6

5

7

8

Page 162: Poscat seminar 9

Strongly Connected Component

Overall procedure

Pick a start vertex

Perform DFS and write the exit time

POSCAT Seminar 1-16214 July 2014

yougatup

1 2

3

4

6

5

7

8

Page 163: Poscat seminar 9

Strongly Connected Component

Overall procedure

Pick a start vertex

Perform DFS and write the exit time

POSCAT Seminar 1-16314 July 2014

yougatup

1 2

3

4

6

5

7

8

I can’t traverse further. Exit !

Page 164: Poscat seminar 9

Strongly Connected Component

Overall procedure

Pick a start vertex

Perform DFS and write the exit time

POSCAT Seminar 1-16414 July 2014

yougatup

1 2

3

4

6

5

7

8

1

Page 165: Poscat seminar 9

Strongly Connected Component

Overall procedure

Pick a start vertex

Perform DFS and write the exit time

POSCAT Seminar 1-16514 July 2014

yougatup

1 2

3

4

6

5

7

8

1

Page 166: Poscat seminar 9

Strongly Connected Component

Overall procedure

Pick a start vertex

Perform DFS and write the exit time

POSCAT Seminar 1-16614 July 2014

yougatup

1 2

3

4

6

5

7

8

1

2

Page 167: Poscat seminar 9

Strongly Connected Component

Overall procedure

Pick a start vertex

Perform DFS and write the exit time

POSCAT Seminar 1-16714 July 2014

yougatup

1 2

3

4

6

5

7

8

1

2

3

Page 168: Poscat seminar 9

Strongly Connected Component

Overall procedure

Pick a start vertex

Perform DFS and write the exit time

POSCAT Seminar 1-16814 July 2014

yougatup

1 2

3

4

6

5

7

8

1

2

3

Page 169: Poscat seminar 9

Strongly Connected Component

Overall procedure

Pick a start vertex

Perform DFS and write the exit time

POSCAT Seminar 1-16914 July 2014

yougatup

1 2

3

4

6

5

7

8

1

2

34

Page 170: Poscat seminar 9

Strongly Connected Component

Overall procedure

Pick a start vertex

Perform DFS and write the exit time

POSCAT Seminar 1-17014 July 2014

yougatup

1 2

3

4

6

5

7

8

1

2

34

5

Page 171: Poscat seminar 9

Strongly Connected Component

Overall procedure

Pick a start vertex

Perform DFS and write the exit time

POSCAT Seminar 1-17114 July 2014

yougatup

1 2

3

4

6

5

7

8

1

2

34

5

Page 172: Poscat seminar 9

Strongly Connected Component

Overall procedure

Pick a start vertex

Perform DFS and write the exit time

POSCAT Seminar 1-17214 July 2014

yougatup

1 2

3

4

6

5

7

8

1

2

34

5

Page 173: Poscat seminar 9

Strongly Connected Component

Overall procedure

Pick a start vertex

Perform DFS and write the exit time

POSCAT Seminar 1-17314 July 2014

yougatup

1 2

3

4

6

5

7

8

1

2

34

5

Page 174: Poscat seminar 9

Strongly Connected Component

Overall procedure

Pick a start vertex

Perform DFS and write the exit time

POSCAT Seminar 1-17414 July 2014

yougatup

1 2

3

4

6

5

7

8

1

2

34

56

Page 175: Poscat seminar 9

Strongly Connected Component

Overall procedure

Pick a start vertex

Perform DFS and write the exit time

POSCAT Seminar 1-17514 July 2014

yougatup

1 2

3

4

6

5

7

8

1

2

34

567

Page 176: Poscat seminar 9

Strongly Connected Component

Overall procedure

Pick a start vertex

Perform DFS and write the exit time

POSCAT Seminar 1-17614 July 2014

yougatup

1 2

3

4

6

5

7

8

1

2

34

567

8

Page 177: Poscat seminar 9

Strongly Connected Component

Overall procedure

After writing all the exit time, reverse whole graph

POSCAT Seminar 1-17714 July 2014

yougatup

1 2

3

4

6

5

7

8

1

2

34

567

8

Page 178: Poscat seminar 9

Strongly Connected Component

Overall procedure

After writing all the exit time, reverse whole graph

POSCAT Seminar 1-17814 July 2014

yougatup

1 2

3

4

6

5

7

8

1

2

34

567

8

Page 179: Poscat seminar 9

Strongly Connected Component

Overall procedure

Perform DFS or BFS started from a vertex with highest exit time

All the vertices reached from the start vertex makes a SCC

POSCAT Seminar 1-17914 July 2014

yougatup

1 2

3

4

6

5

7

8

1

2

34

567

8

Page 180: Poscat seminar 9

Strongly Connected Component

Overall procedure

Perform DFS or BFS started from a vertex with highest exit time

All the vertices reached from the start vertex makes a SCC

POSCAT Seminar 1-18014 July 2014

yougatup

1 2

3

4

6

5

7

8

1

2

34

567

8

Page 181: Poscat seminar 9

Strongly Connected Component

Overall procedure

Perform DFS or BFS started from a vertex with highest exit time

All the vertices reached from the start vertex makes a SCC

POSCAT Seminar 1-18114 July 2014

yougatup

1 2

3

4

6

5

7

8

1

2

34

567

8

Page 182: Poscat seminar 9

Strongly Connected Component

Overall procedure

Perform DFS or BFS started from a vertex with highest exit time

All the vertices reached from the start vertex makes a SCC

POSCAT Seminar 1-18214 July 2014

yougatup

1 2

3

4

6

5

7

8

1

2

34

567

8

Page 183: Poscat seminar 9

Strongly Connected Component

Overall procedure

Perform DFS or BFS started from a vertex with highest exit time

All the vertices reached from the start vertex makes a SCC

POSCAT Seminar 1-18314 July 2014

yougatup

1 2

3

4

6

5

7

8

1

2

34

567

8

Page 184: Poscat seminar 9

Strongly Connected Component

Overall procedure

Perform DFS or BFS started from a vertex with highest exit time

All the vertices reached from the start vertex makes a SCC

POSCAT Seminar 1-18414 July 2014

yougatup

1 2

3

4

6

5

7

8

1

2

34

567

8

Page 185: Poscat seminar 9

Strongly Connected Component

Overall procedure

Perform DFS or BFS started from a vertex with highest exit time

All the vertices reached from the start vertex makes a SCC

POSCAT Seminar 1-18514 July 2014

yougatup

1 2

3

4

6

5

7

8

1

2

34

567

8

Page 186: Poscat seminar 9

Strongly Connected Component

Overall procedure

Perform DFS or BFS started from a vertex with highest exit time

All the vertices reached from the start vertex makes a SCC

POSCAT Seminar 1-18614 July 2014

yougatup

1 2

3

4

6

5

7

8

1

2

34

567

8

Page 187: Poscat seminar 9

Strongly Connected Component

Overall procedure

Perform DFS or BFS started from a vertex with highest exit time

All the vertices reached from the start vertex makes a SCC

POSCAT Seminar 1-18714 July 2014

yougatup

1 2

3

4

6

5

7

8

1

2

34

567

8

Page 188: Poscat seminar 9

Strongly Connected Component

Overall procedure

Perform DFS or BFS started from a vertex with highest exit time

All the vertices reached from the start vertex makes a SCC

POSCAT Seminar 1-18814 July 2014

yougatup

1 2

3

4

6

5

7

8

1

2

34

567

8

Done !

Page 189: Poscat seminar 9

Strongly Connected Component

Analysis

It is correct ?

How long does it takes ?

POSCAT Seminar 1-18914 July 2014

yougatup

1 2

3

4

6

5

7

8

1

2

34

567

8

Done !

Page 190: Poscat seminar 9

Strongly Connected Component

Analysis

It is correct ? We need some detail proof

How long does it takes ? We need 2 DFS (or 1 DFS + 1 BFS)

POSCAT Seminar 1-19014 July 2014

yougatup

1 2

3

4

6

5

7

8

1

2

34

567

8

Done !

Page 191: Poscat seminar 9

Strongly Connected Component

Analysis

It is correct ? We need some detail proof

How long does it takes ? O(𝑽 + 𝑬)

POSCAT Seminar 1-19114 July 2014

yougatup

1 2

3

4

6

5

7

8

1

2

34

567

8

Done !