Poscat seminar 9

Preview:

Citation preview

POSCAT Seminar 9 :Graph 1

yougatup @ POSCAT

1

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

Graph

Basic Concept

− 𝐺 = (𝑉, 𝐸)

− A set of vertices and edges

POSCAT Seminar 1-314 July 2014

yougatup

1 2

3

4

6

5

7

8

Graph

Terminology

− Nodes

POSCAT Seminar 1-414 July 2014

yougatup

1 2

3

4

6

5

7

8

Graph

Terminology

− Edges

POSCAT Seminar 1-514 July 2014

yougatup

1 2

3

4

6

5

7

8

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

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

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

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

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

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 ?

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

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 ?

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!

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

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 ?

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

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 ?

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

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

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 𝑂(𝑉)

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

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

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

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

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

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 ?

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

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

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

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

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 ?

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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 ?

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 ?

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.

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.

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 ?

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 !

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

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

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

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

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 !

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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 ?

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!

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

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

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

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

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

Strongly Connected Component

Problem

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

POSCAT Seminar 1-13014 July 2014

yougatup

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Strongly Connected Component

Problem

OTL …

POSCAT Seminar 1-15114 July 2014

yougatup

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

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

Strongly Connected Component

Problem

OTL …

And reverse the whole graph !

POSCAT Seminar 1-15414 July 2014

yougatup

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

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

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

Strongly Connected Component

Overall procedure

POSCAT Seminar 1-15814 July 2014

yougatup

1 2

3

4

6

5

7

8

Strongly Connected Component

Overall procedure

Pick a start vertex

POSCAT Seminar 1-15914 July 2014

yougatup

1 2

3

4

6

5

7

8

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

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

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

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 !

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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 !

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 !

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 !

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 !