• 検索結果がありません。

I111E Algorithms & Data Structures10. Graph Algorithms (1): Graph Representations, Breadth-First Search and Depth-First Search

N/A
N/A
Protected

Academic year: 2021

シェア "I111E Algorithms & Data Structures10. Graph Algorithms (1): Graph Representations, Breadth-First Search and Depth-First Search"

Copied!
24
0
0

読み込み中.... (全文を見る)

全文

(1)

I111E Algorithms & Data Structures 10. Graph Algorithms (1):

Graph Representations, Breadth- First Search and Depth-First Search

1

School of Information Science Ryuhei Uehara & Giovanni Viglietta

[email protected] & [email protected] 2019-11-20

All materials are available at

http://www.jaist.ac.jp/~uehara/couse/2019/i111e

C Version

(2)

• “Vertices” (nodes) are joined by edges (arcs)

– Directed graph: each edge has direction

– Undirected graph: each edge has no direction

Shinjuku Ikebukuro Ueno

Example:

relationship between topics

Graph

Example: railway in Tokyo

Tokyo

Array Sort

(3)

• Graph G = (V, E)

V: vertex set, E: edge set

• Vertices: u, v, … ∈ V

• Edges: e = {u, v} ∈ E

(undirected) a = (u, v) ∈ E

(directed)

• Weighted variants;

w(u), w(e)

– Distance, cost, time, etc.

u1

u2 u10

u3 u4

u9 u6 u7

u5

u8

Tokyo Ueno Ikebukuro

Shinjuku

3

Graph: Notation

(4)

Graph: basic notions/notations (1/2)

• Path: sequence of vertices joined by edges

– Simple path: it never visit the same vertex again

• Cycle, closed path: path from v to v

• Connected graph: Every pair of vertices is

joined by a path

(5)

Graph: basic notions/notations (2/2)

• Forest: Graph with no cycle (acyclic)

• Tree: Connected acyclic graph

• Complete graph: Every pair of vertices is

connected by an edge, denoted by K

n

, where n is the number of vertices.

– Example: K

5

5

(6)

Computational complexity of graph problems

• The number n of vertices, the number m of edges;

– Undirected graph: m ≦ n(n-1)/2 – Directed graph: m ≦ n(n-1)

• m ∈ O(n

2

)

• Every tree has m=n-1 edges, so m ∈ O(n).

• Computational complexity of graph algorithm is

described by equations of n and m.

(7)

Representative representations of a graph in computer

• Adjacency matrix

• Adjacency list

1 2 3

4 4

2 3

4 2 Vertex

(array of pointers)

List of neighbors

1 2

3

4

7

(8)

1 2

3

4

Representation of a graph:

matrix representation (adjacency matrix)

• (u, v) ∈ EM[u, v] = 1

• (u, v) ∉ EM[u, v] = 0 It is easy to extend

edge-weighted graph.

(9)

• (u, v) ∈ EvL(u)

L(u) is the list of neighbors of u

1 2 3

4 4

2 3

4 2 Vertex

(array of pointers)

List of neighbors

Representation of a graph:

list representation (adjacency list)

9

It is easy to extend vertex-weighted graph.

1 2

3

4

(10)

• Space complexity

– Adjacency matrix: Θ(n

2

) – Adjacency list: Θ(m log n)

• Time complexity of checking if (u, v) ∈ E ?

– Adjacency matrix: Θ(1) – Adjacency list : Θ(n)

Adj. matrix v.s. Adj. list

(11)

Search in Graph

11

(12)

Search in Graph

• How can we check all vertices in a graph systematically ,

and solve some problem?

– e.g., Do you have a path from A to D?

• Two major (efficient) algorithms:

– Breadth-First Search: A -> B -> C -> D -> F -> E it starts from a vertex v, and visit all (reachable) vertices from the vertices closer to v.

– Depth-First Search: A -> B -> D -> E -> C -> F

it starts from a vertex v, and visit every reachable

A

B C

D

E

F

(13)

BFS (Breadth-First Search)

• For a graph G=(V,E) and any start point s ∈ V, all reachable vertices from s will be visited from s in order of distance from s.

• Outline of method: color all vertices by white, gray, or black as follows;

– White: Unvisited vertex

– Gray: It is visited, but it has unvisited neighbors

– Black: It is already visited, and all neighbors are also visited

– Search is completed when all vertices got black

– Color of each vertex is changed as white  gray  black

13

(14)

BFS(V,E,s){

for v ∈ V do toWhite(v); endfor toGray(s);

Q={s};

while( Q!={} ){

u=pop(Q); // Q  Q’ where Q={u} ∪ Q’

for v ∈ {v ∈ V|(v,u) ∈ E}

if isWhite(v) then

toGray(v); push(Q,v);

endif endfor

BFS (Breadth-First Search):

Program code

Queue is the best structure!

Take u from left (the first gray node)

If neighbor v of u is white,

Push v to the right of Q

(processed lastly)

(15)

1 2 3

4 5 6

Q={1}

1 2 3

4 5 6

u=1, visit 2 Q={2}

black 1

1 2 3

4 5 6

u=2,

visit 3,4,5 Q={3,4,5}

black 2

1 2 3

4 5 6

u=3, visit null Q={4,5}

black 3

1 2 3

4 5 6

u=4, visit null Q={5}

black 4

1 2 3

4 5 6

u=5, visit 6 Q={6}

black 5

1 2 3

4 5 6

u=6, visit null Q={}black 6

BFS (Breadth-First Search): Example

15

(16)

Time complexity is not easy from program…

BFS:

Time complexity BFS(V,E,s){

for v ∈ V do

toWhite(v);

endfor

toGray(s);

Q={s};

while( Q!={} ){

u=pop(Q);

for v ∈ {v ∈ V|(v,u) ∈ E}

if isWhite(v) then toGray(v);

push(Q,v);

endif endfor

Consider from the

viewpoints of vertices &

edges

• Each vertex never gets white again after initialization.

• Each vertex gets into Q and gets out from Q at most once

• Each edge is checked at most once

– when one endpoint vertex is

taken from Q and its neighbors are checked along edges

(17)

Application of BFS:

Shortest path problem on graph

17

Definition of “distance”

– Start vertex v has distance 0

– Except start vertex, each vertex u has distance d+1, where d is the distance of parent of u.

• On BFS, modify that each gray vertex receives

its “distance” from black neighbor, then you

get (shortest) distance from v to it.

(18)

DFS (Depth-First Search)

• For a graph G=(V,E) and start point s ∈ V, it follows reachable vertices from s until it reaches a vertex that has no unvisited

neighbor , and returns to the last vertex that has unvisited neighbors.

dfs(V, E, s) {

visit(s) // make gray for (s, w) ∈ E do

if notVisited(w) then dfs(V, E, w)

Program code is

relatively simple, and

vertices are put into a

stack when dfs makes a

(19)

DFS: Example

1 2 3

4 5 6

DFS(1)

1 2 3

4 5 6

DFS(2)

1 2 3

4 5 6

DFS(3)

1 2 3

4 5 6

DFS(5)

1 2 3

4 5 6

DFS(6)

2

1 3

4 5 6

DFS(6)

1 2 3

4 5 6

DFS(5) DFS(3) DFS(2)

1 2 3

4 5 6

DFS(4)

1 2 3

4 5 6

DFS(2)

19

(20)

DFS(V,E,s){

for v ∈ V do toWhite(v); endfor toGray(s);

S={s};

while( S!={} ){

u=pop(S);

for v ∈ {v ∈ V|(u,v) ∈ E}

if isnotBlack(v) then toGray(v); push(S,v);

endif endfor

DFS non-recursive version

: We can use stack explicitly to search a tree

stack of gray nodes

Pop u from top (last node in gray) If neighbor v of u is not black

Push v into S on top

(which will be processed

at first )

(21)

Example (non-rec. ver.)

1 2 3

4 5 6

S={1}

1 2 3

4 5 6

u=1visit 2 S={2}

black 1

1 2 3

4 5 6

u=2visit 5,4,3 S={5,4,3}

black 2

1 2 3

4 5 6

u=3visit 5 S={5,4,5}

black 3

1 2 3

4 5 6

u=5visit 6 S={5,4,6}

black 5

2

1 3

4 5 6

u=6visit null S={5,4}

black 6

1 2 3

4 5 6

u=4visit null S={5}

black 4

1 2 3

4 5 6

u=5visit null S={}

21

(22)

• For a given (disconnected) graph G = (V, E),

divide it into connected graphs G

1

= (V

1

, E

1

), …, G

c

= (V

c

, E

c

).

– We will give a numbering array cn[] such that

u,vV, uV

i

vV

j

i≠jcn[u] ≠ cn[v]

Application of DFS:

Find connected components in a graph

1

1 1

2 2

G

1

G

2

G

(23)

cc(V,E,cn){ //cn[|V|]

for v ∈ V do

cn[v] = 0; /*initialize*/

endfor k = 1;

for v ∈ V do

if cn[v]==0 then dfs(V,E,v,k,cn);

k=k+1;

endif endfor }

dfs(V,E,v,k,cn){

cn[v]=k;

for u ∈ {u|(v,u) ∈ E} do if cn[u]==0 then

dfs(V,E,u,k,cn);

endif endfor }

Application of DFS:

Find connected components of a graph

23

(24)

BFS v.s. DFS on a graph

• Two major (efficient) algorithms:

– Breadth-First Search:

It is easy to implement by “ queue”

– Depth-First Search:

It is easy to implement by “stack”

– Both algorithms are easy to implement to run in O(|V|+|E|) time if you use reasonable data

representation and data structure. (This time

complexity is optimal since you have to check all input

参照

関連したドキュメント

We show how the tau constant changes under graph oper- ations such as the deletion of an edge, the contraction of an edge into its end points, identifying any two vertices,

Intervals graphs (denoted by INT ) are intersection graphs of intervals on a line, circular-arc graphs (CA ) are intersection graphs of intervals (arcs) on a circle, circle graphs (CI

A Tabu search procedure is then used to select a subset of financial ratio variables which best predict bankruptcy from among a larger initial set of 20 variables, and use that

Zheng and Yan 7 put efforts into using forward search in planning graph algorithm to solve WSC problem, and it shows a good result which can find a solution in polynomial time

Under certain assumptions on the sequence (P N ) N≥0 (which still allow for the standard probabilis- tic models of algorithms associated with binary search trees, digital search

1-regular pentavalent graph (that is, the full automorphism group acts regularly on its arc set) of square-free order is presented in [12], and all the possibilities of

Then X admits the structure of a graph of spaces, where all the vertex and edge spaces are (n − 1) - dimensional FCCs and the maps from edge spaces to vertex spaces are combi-

The layout produced by the VDCB algorithm is more evenly distributed according to the CP model, and is more similar to the original layout according to the similarity measures