44
1 SWE2004: Principles in Programming | Spring 2014 | Euiseong Seo ([email protected]) Lecture 9 Graph Traversal Euiseong Seo ([email protected])

Lecture 9 Graph Traversal - KOCWcontents.kocw.net/KOCW/document/2015/sungkyunkwan/suhuiseon… · SWE2004: Principles in Programming | Spring 2014 | Euiseong Seo ([email protected])

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Lecture 9 Graph Traversal - KOCWcontents.kocw.net/KOCW/document/2015/sungkyunkwan/suhuiseon… · SWE2004: Principles in Programming | Spring 2014 | Euiseong Seo (euiseong@skku.edu)

1 SWE2004: Principles in Programming | Spring 2014 | Euiseong Seo ([email protected])

Lecture 9 Graph Traversal

Euiseong Seo

([email protected])

Page 2: Lecture 9 Graph Traversal - KOCWcontents.kocw.net/KOCW/document/2015/sungkyunkwan/suhuiseon… · SWE2004: Principles in Programming | Spring 2014 | Euiseong Seo (euiseong@skku.edu)

2 SWE2004: Principles in Programming | Spring 2014 | Euiseong Seo ([email protected])

Need for Graphs

One of unifying themes of computer science

Closely related to many daily life problems

• Navigation

• Circuit generation

• Social network services

• Games

• Computer networks

How can a problem be represented as a graph?

How to solve a graph problem?

Page 3: Lecture 9 Graph Traversal - KOCWcontents.kocw.net/KOCW/document/2015/sungkyunkwan/suhuiseon… · SWE2004: Principles in Programming | Spring 2014 | Euiseong Seo (euiseong@skku.edu)

3 SWE2004: Principles in Programming | Spring 2014 | Euiseong Seo ([email protected])

Graph Notation

A graph G = (V, E)

• V is a set of vertices(nodes)

• E is a set of edges – E = (x, y) where x, y V

– Ordered or unordered pairs of vertices from V

Modeling of problems – What are vertices and edges in the followings? • Road networks

• Human interactions

• Program analysis

Page 4: Lecture 9 Graph Traversal - KOCWcontents.kocw.net/KOCW/document/2015/sungkyunkwan/suhuiseon… · SWE2004: Principles in Programming | Spring 2014 | Euiseong Seo (euiseong@skku.edu)

4 SWE2004: Principles in Programming | Spring 2014 | Euiseong Seo ([email protected])

Flavors of Graphs

Undirected or directed

• A graph is undirected if edge (x, y) E implies that (y,x) E, too

• Otherwise, the graph is directed

w x

z y

w x

z y

Page 5: Lecture 9 Graph Traversal - KOCWcontents.kocw.net/KOCW/document/2015/sungkyunkwan/suhuiseon… · SWE2004: Principles in Programming | Spring 2014 | Euiseong Seo (euiseong@skku.edu)

5 SWE2004: Principles in Programming | Spring 2014 | Euiseong Seo ([email protected])

Flavors of Graphs

Weighted or unweighted

• If each edge of a graph is assigned a numerical value, or weight, the graph is a weighted graph

• Otherwise, it is a unweighted graph

w x

z y

5

3

1

7

w x

z y

1

1

1

1

Page 6: Lecture 9 Graph Traversal - KOCWcontents.kocw.net/KOCW/document/2015/sungkyunkwan/suhuiseon… · SWE2004: Principles in Programming | Spring 2014 | Euiseong Seo (euiseong@skku.edu)

6 SWE2004: Principles in Programming | Spring 2014 | Euiseong Seo ([email protected])

Flavors of Graphs

Cyclic or acyclic

• An acyclic graph does not contain any cycles

• Trees are connected acyclic undirected graphs

• Directed acyclic graphs are called DAGs

Page 7: Lecture 9 Graph Traversal - KOCWcontents.kocw.net/KOCW/document/2015/sungkyunkwan/suhuiseon… · SWE2004: Principles in Programming | Spring 2014 | Euiseong Seo (euiseong@skku.edu)

7 SWE2004: Principles in Programming | Spring 2014 | Euiseong Seo ([email protected])

Data Structures for Graphs

Assume that a graph G = (V, E) contains n vertices and m edges

Adjacency matrix

• Use a n x n matrix M

• M[i,j] = 1, if (i,j) E

• M[i,j] = 0, if (i,j) E

• Pros – Easy to add or remove edges

– Easy to find a specific edge, (i,j), if exists

• Cons – Waste of memory space for sparse graphs

Page 8: Lecture 9 Graph Traversal - KOCWcontents.kocw.net/KOCW/document/2015/sungkyunkwan/suhuiseon… · SWE2004: Principles in Programming | Spring 2014 | Euiseong Seo (euiseong@skku.edu)

8 SWE2004: Principles in Programming | Spring 2014 | Euiseong Seo ([email protected])

Data Structures for Graphs

Adjacency lists in lists

1 2

5 4

3

1 2

5 4

3

2 5 / 1

1 4 / 2 5 3

2 4 / 3

2 3 / 4 5

4 2 / 5 1

2 5 / 1

5 / 2

3 4 / 3

4 / 5

Page 9: Lecture 9 Graph Traversal - KOCWcontents.kocw.net/KOCW/document/2015/sungkyunkwan/suhuiseon… · SWE2004: Principles in Programming | Spring 2014 | Euiseong Seo (euiseong@skku.edu)

9 SWE2004: Principles in Programming | Spring 2014 | Euiseong Seo ([email protected])

Data Structures for Graphs

Adjacency lists in matrices

• Use arrays instead of linked lists

• Looks like it combines the worst properties of both, but.

1 2

5 4

3

1 2 5 1 2

2 1 5 1 2

4 3 3 4

3 2 4 1 2

4 2 5 1 2

3 3

5 1 2 1 2

4 3

1 2

5 4

3

1 2 5 1 2

2 5 1

3 3 4 1 2

5 4 1

Page 10: Lecture 9 Graph Traversal - KOCWcontents.kocw.net/KOCW/document/2015/sungkyunkwan/suhuiseon… · SWE2004: Principles in Programming | Spring 2014 | Euiseong Seo (euiseong@skku.edu)

10 SWE2004: Principles in Programming | Spring 2014 | Euiseong Seo ([email protected])

List in Array Representation

An undirected edge (x,y) appears twice, once as y in x’s list and once as x in y’s list

Page 11: Lecture 9 Graph Traversal - KOCWcontents.kocw.net/KOCW/document/2015/sungkyunkwan/suhuiseon… · SWE2004: Principles in Programming | Spring 2014 | Euiseong Seo (euiseong@skku.edu)

11 SWE2004: Principles in Programming | Spring 2014 | Euiseong Seo ([email protected])

Tree Traversal

BFS (Breadth First Search)

DFS (Depth First Search)

Page 12: Lecture 9 Graph Traversal - KOCWcontents.kocw.net/KOCW/document/2015/sungkyunkwan/suhuiseon… · SWE2004: Principles in Programming | Spring 2014 | Euiseong Seo (euiseong@skku.edu)

12 SWE2004: Principles in Programming | Spring 2014 | Euiseong Seo ([email protected])

BFS

s

2

5

4

7

8

3 6 9

Page 13: Lecture 9 Graph Traversal - KOCWcontents.kocw.net/KOCW/document/2015/sungkyunkwan/suhuiseon… · SWE2004: Principles in Programming | Spring 2014 | Euiseong Seo (euiseong@skku.edu)

13 SWE2004: Principles in Programming | Spring 2014 | Euiseong Seo ([email protected])

s

2

5

4

7

8

3 6 9

0

Undiscovered

Discovered

Finished

Queue: s

Top of queue

2 1

Shortest path

from s

2

Page 14: Lecture 9 Graph Traversal - KOCWcontents.kocw.net/KOCW/document/2015/sungkyunkwan/suhuiseon… · SWE2004: Principles in Programming | Spring 2014 | Euiseong Seo (euiseong@skku.edu)

14 SWE2004: Principles in Programming | Spring 2014 | Euiseong Seo ([email protected])

s

2

5

4

7

8

3 6 9

0

Queue: s 2

3

1

1

Undiscovered

Discovered

Finished

Top of queue

3

Page 15: Lecture 9 Graph Traversal - KOCWcontents.kocw.net/KOCW/document/2015/sungkyunkwan/suhuiseon… · SWE2004: Principles in Programming | Spring 2014 | Euiseong Seo (euiseong@skku.edu)

15 SWE2004: Principles in Programming | Spring 2014 | Euiseong Seo ([email protected])

s

2

5

4

7

8

3 6 9

0

Queue: s 2 3

5

1

1

1

Undiscovered

Discovered

Finished

Top of queue

5

Page 16: Lecture 9 Graph Traversal - KOCWcontents.kocw.net/KOCW/document/2015/sungkyunkwan/suhuiseon… · SWE2004: Principles in Programming | Spring 2014 | Euiseong Seo (euiseong@skku.edu)

16 SWE2004: Principles in Programming | Spring 2014 | Euiseong Seo ([email protected])

s

2

5

4

7

8

3 6 9

0

Queue: 2 3 5

4 1

1

1

2

Undiscovered

Discovered

Finished

Top of queue

4

Page 17: Lecture 9 Graph Traversal - KOCWcontents.kocw.net/KOCW/document/2015/sungkyunkwan/suhuiseon… · SWE2004: Principles in Programming | Spring 2014 | Euiseong Seo (euiseong@skku.edu)

17 SWE2004: Principles in Programming | Spring 2014 | Euiseong Seo ([email protected])

s

2

5

4

7

8

3 6 9

0

Queue: 2 3 5 4

1

1

1

2

5 already discovered:

don't enqueue

Undiscovered

Discovered

Finished

Top of queue

Page 18: Lecture 9 Graph Traversal - KOCWcontents.kocw.net/KOCW/document/2015/sungkyunkwan/suhuiseon… · SWE2004: Principles in Programming | Spring 2014 | Euiseong Seo (euiseong@skku.edu)

18 SWE2004: Principles in Programming | Spring 2014 | Euiseong Seo ([email protected])

s

2

5

4

7

8

3 6 9

0

Queue: 2 3 5 4

1

1

1

2

Undiscovered

Discovered

Finished

Top of queue

Page 19: Lecture 9 Graph Traversal - KOCWcontents.kocw.net/KOCW/document/2015/sungkyunkwan/suhuiseon… · SWE2004: Principles in Programming | Spring 2014 | Euiseong Seo (euiseong@skku.edu)

19 SWE2004: Principles in Programming | Spring 2014 | Euiseong Seo ([email protected])

s

2

5

4

7

8

3 6 9

0

Queue: 3 5 4

1

1

1

2

Undiscovered

Discovered

Finished

Top of queue

Page 20: Lecture 9 Graph Traversal - KOCWcontents.kocw.net/KOCW/document/2015/sungkyunkwan/suhuiseon… · SWE2004: Principles in Programming | Spring 2014 | Euiseong Seo (euiseong@skku.edu)

20 SWE2004: Principles in Programming | Spring 2014 | Euiseong Seo ([email protected])

s

2

5

4

7

8

3 6 9

0

Queue: 3 5 4

1

1

1

2

6

2

Undiscovered

Discovered

Finished

Top of queue

6

Page 21: Lecture 9 Graph Traversal - KOCWcontents.kocw.net/KOCW/document/2015/sungkyunkwan/suhuiseon… · SWE2004: Principles in Programming | Spring 2014 | Euiseong Seo (euiseong@skku.edu)

21 SWE2004: Principles in Programming | Spring 2014 | Euiseong Seo ([email protected])

s

2

5

4

7

8

3 6 9

0

Queue: 5 4 6

1

1

1

2

2

Undiscovered

Discovered

Finished

Top of queue

Page 22: Lecture 9 Graph Traversal - KOCWcontents.kocw.net/KOCW/document/2015/sungkyunkwan/suhuiseon… · SWE2004: Principles in Programming | Spring 2014 | Euiseong Seo (euiseong@skku.edu)

22 SWE2004: Principles in Programming | Spring 2014 | Euiseong Seo ([email protected])

s

2

5

4

7

8

3 6 9

0

Queue: 4 6

1

1

1

2

2

Undiscovered

Discovered

Finished

Top of queue

Page 23: Lecture 9 Graph Traversal - KOCWcontents.kocw.net/KOCW/document/2015/sungkyunkwan/suhuiseon… · SWE2004: Principles in Programming | Spring 2014 | Euiseong Seo (euiseong@skku.edu)

23 SWE2004: Principles in Programming | Spring 2014 | Euiseong Seo ([email protected])

s

2

5

4

7

8

3 6 9

0

Queue: 4 6

1

1

1

2

2

8 3

Undiscovered

Discovered

Finished

Top of queue

8

Page 24: Lecture 9 Graph Traversal - KOCWcontents.kocw.net/KOCW/document/2015/sungkyunkwan/suhuiseon… · SWE2004: Principles in Programming | Spring 2014 | Euiseong Seo (euiseong@skku.edu)

24 SWE2004: Principles in Programming | Spring 2014 | Euiseong Seo ([email protected])

s

2

5

4

7

8

3 6 9

0

Queue: 6 8

1

1

1

2

2

3

7

3

Undiscovered

Discovered

Finished

Top of queue

7

Page 25: Lecture 9 Graph Traversal - KOCWcontents.kocw.net/KOCW/document/2015/sungkyunkwan/suhuiseon… · SWE2004: Principles in Programming | Spring 2014 | Euiseong Seo (euiseong@skku.edu)

25 SWE2004: Principles in Programming | Spring 2014 | Euiseong Seo ([email protected])

s

2

5

4

7

8

3 6 9

0

Queue: 6 8 7

1

1

1

2

2

3

9

3

3

Undiscovered

Discovered

Finished

Top of queue

9

Page 26: Lecture 9 Graph Traversal - KOCWcontents.kocw.net/KOCW/document/2015/sungkyunkwan/suhuiseon… · SWE2004: Principles in Programming | Spring 2014 | Euiseong Seo (euiseong@skku.edu)

26 SWE2004: Principles in Programming | Spring 2014 | Euiseong Seo ([email protected])

s

2

5

4

7

8

3 6 9

0

Queue: 8 7 9

1

1

1

2

2

3

9

3

3

Undiscovered

Discovered

Finished

Top of queue

Page 27: Lecture 9 Graph Traversal - KOCWcontents.kocw.net/KOCW/document/2015/sungkyunkwan/suhuiseon… · SWE2004: Principles in Programming | Spring 2014 | Euiseong Seo (euiseong@skku.edu)

27 SWE2004: Principles in Programming | Spring 2014 | Euiseong Seo ([email protected])

s

2

5

4

7

8

3 6 9

0

Queue: 7 9

1

1

1

2

2

3

3

3

Undiscovered

Discovered

Finished

Top of queue

Page 28: Lecture 9 Graph Traversal - KOCWcontents.kocw.net/KOCW/document/2015/sungkyunkwan/suhuiseon… · SWE2004: Principles in Programming | Spring 2014 | Euiseong Seo (euiseong@skku.edu)

28 SWE2004: Principles in Programming | Spring 2014 | Euiseong Seo ([email protected])

s

2

5

4

7

8

3 6 9

0

Queue: 7 9

1

1

1

2

2

3

3

3

Undiscovered

Discovered

Finished

Top of queue

Page 29: Lecture 9 Graph Traversal - KOCWcontents.kocw.net/KOCW/document/2015/sungkyunkwan/suhuiseon… · SWE2004: Principles in Programming | Spring 2014 | Euiseong Seo (euiseong@skku.edu)

29 SWE2004: Principles in Programming | Spring 2014 | Euiseong Seo ([email protected])

s

2

5

4

7

8

3 6 9

0

Queue: 7 9

1

1

1

2

2

3

3

3

Undiscovered

Discovered

Finished

Top of queue

Page 30: Lecture 9 Graph Traversal - KOCWcontents.kocw.net/KOCW/document/2015/sungkyunkwan/suhuiseon… · SWE2004: Principles in Programming | Spring 2014 | Euiseong Seo (euiseong@skku.edu)

30 SWE2004: Principles in Programming | Spring 2014 | Euiseong Seo ([email protected])

s

2

5

4

7

8

3 6 9

0

Queue: 9

1

1

1

2

2

3

3

3

Undiscovered

Discovered

Finished

Top of queue

Page 31: Lecture 9 Graph Traversal - KOCWcontents.kocw.net/KOCW/document/2015/sungkyunkwan/suhuiseon… · SWE2004: Principles in Programming | Spring 2014 | Euiseong Seo (euiseong@skku.edu)

31 SWE2004: Principles in Programming | Spring 2014 | Euiseong Seo ([email protected])

s

2

5

4

7

8

3 6 9

0

Queue: 9

1

1

1

2

2

3

3

3

Undiscovered

Discovered

Finished

Top of queue

Page 32: Lecture 9 Graph Traversal - KOCWcontents.kocw.net/KOCW/document/2015/sungkyunkwan/suhuiseon… · SWE2004: Principles in Programming | Spring 2014 | Euiseong Seo (euiseong@skku.edu)

32 SWE2004: Principles in Programming | Spring 2014 | Euiseong Seo ([email protected])

s

2

5

4

7

8

3 6 9

0

Queue:

1

1

1

2

2

3

3

3

Undiscovered

Discovered

Finished

Top of queue Since Queue is empty, STOP!

Page 33: Lecture 9 Graph Traversal - KOCWcontents.kocw.net/KOCW/document/2015/sungkyunkwan/suhuiseon… · SWE2004: Principles in Programming | Spring 2014 | Euiseong Seo (euiseong@skku.edu)

33 SWE2004: Principles in Programming | Spring 2014 | Euiseong Seo ([email protected])

DFS

Similar to Backtracking • Go as deep as you can • Next one is your siblings

Stack is an ideal candidate DFS(G, v)

for all edges e incident on v

do if edge e is unexplored then

w opposite(v, e) // return the end point of e distant to v

if vertex w is unexplored then

mark e as a discovered edge

recursively call DFS(G, w)

else

mark e as a back edge

Page 34: Lecture 9 Graph Traversal - KOCWcontents.kocw.net/KOCW/document/2015/sungkyunkwan/suhuiseon… · SWE2004: Principles in Programming | Spring 2014 | Euiseong Seo (euiseong@skku.edu)

34 SWE2004: Principles in Programming | Spring 2014 | Euiseong Seo ([email protected])

Finding Paths

BFS Tree from x is unique

Parent[i] is the node that discovered node i during the BFS originated from x

Finding the shortest path from x to y in a undirected graph

• By following the chain of ancestors backward from y to the root

Page 35: Lecture 9 Graph Traversal - KOCWcontents.kocw.net/KOCW/document/2015/sungkyunkwan/suhuiseon… · SWE2004: Principles in Programming | Spring 2014 | Euiseong Seo (euiseong@skku.edu)

35 SWE2004: Principles in Programming | Spring 2014 | Euiseong Seo ([email protected])

Connected Components

Many seemingly complicated problems reduce to

finding connected components

• 15-Puzzle

Connected components can be found by using repetitive application of DFS or BFS

Page 36: Lecture 9 Graph Traversal - KOCWcontents.kocw.net/KOCW/document/2015/sungkyunkwan/suhuiseon… · SWE2004: Principles in Programming | Spring 2014 | Euiseong Seo (euiseong@skku.edu)

36 SWE2004: Principles in Programming | Spring 2014 | Euiseong Seo ([email protected])

Topological Sorting

One of the fundamental operations on DAGs

Construct an ordering of the vertices such that all directed edges go from left to right

• Cannot exist over cyclic graphs

This gives us a way to process each vertex before any of its successors

• Suppose we seek the shortest (or longest) path from x to y

• No vertex appearing after y in the topological order can contribute to any such path

Page 37: Lecture 9 Graph Traversal - KOCWcontents.kocw.net/KOCW/document/2015/sungkyunkwan/suhuiseon… · SWE2004: Principles in Programming | Spring 2014 | Euiseong Seo (euiseong@skku.edu)

37 SWE2004: Principles in Programming | Spring 2014 | Euiseong Seo ([email protected])

Topological Sorting Algorithm

Definition

• A topological sort of a DAG G is a linear ordering of all its vertices such that if G contains a link (u,v), then node u appears before node v in the ordering

b

c a

d

b

c a

d

1

2 3

4

Page 38: Lecture 9 Graph Traversal - KOCWcontents.kocw.net/KOCW/document/2015/sungkyunkwan/suhuiseon… · SWE2004: Principles in Programming | Spring 2014 | Euiseong Seo (euiseong@skku.edu)

38 SWE2004: Principles in Programming | Spring 2014 | Euiseong Seo ([email protected])

Topological Sorting Algorithm

find source nodes (indegree = 0)

• if there is no such node, the graph is NOT DAG

c

a

b

e

d

f

in_deg=1

in_deg=0

in_deg=2

in_deg=1

in_deg=3

in_deg=1 Queue

Sorted: -

c

Page 39: Lecture 9 Graph Traversal - KOCWcontents.kocw.net/KOCW/document/2015/sungkyunkwan/suhuiseon… · SWE2004: Principles in Programming | Spring 2014 | Euiseong Seo (euiseong@skku.edu)

39 SWE2004: Principles in Programming | Spring 2014 | Euiseong Seo ([email protected])

Topological Sorting Algorithm

span c; decrement in_deg of a, b, e

• store a in Queue since in_deg becomes 0

c

a

b

e

d

f

in_deg=0

in_deg=0

in_deg=1

in_deg=1

in_deg=2

in_deg=1 Queue

Sorted: c

c

a

Page 40: Lecture 9 Graph Traversal - KOCWcontents.kocw.net/KOCW/document/2015/sungkyunkwan/suhuiseon… · SWE2004: Principles in Programming | Spring 2014 | Euiseong Seo (euiseong@skku.edu)

40 SWE2004: Principles in Programming | Spring 2014 | Euiseong Seo ([email protected])

span a; decrement in_deg of b, f

• store b, f in Queue since ...

c

a

b

e

d

f

in_deg=0

in_deg=0

in_deg=0

in_deg=0

in_deg=2

in_deg=1 Queue

Sorted: c a

a

b

f

Topological Sorting Algorithm

Page 41: Lecture 9 Graph Traversal - KOCWcontents.kocw.net/KOCW/document/2015/sungkyunkwan/suhuiseon… · SWE2004: Principles in Programming | Spring 2014 | Euiseong Seo (euiseong@skku.edu)

41 SWE2004: Principles in Programming | Spring 2014 | Euiseong Seo ([email protected])

span b; store d in Queue

c

a

b

e

d

f

in_deg=0

in_deg=0

in_deg=0

in_deg=0

in_deg=2

in_deg=0 Queue

Sorted: c a b

b

f

d

Topological Sorting Algorithm

Page 42: Lecture 9 Graph Traversal - KOCWcontents.kocw.net/KOCW/document/2015/sungkyunkwan/suhuiseon… · SWE2004: Principles in Programming | Spring 2014 | Euiseong Seo (euiseong@skku.edu)

42 SWE2004: Principles in Programming | Spring 2014 | Euiseong Seo ([email protected])

span f; decrement in_deg of e • no node with in_deg = 0 is found

c

a

b

e

d

f

in_deg=0

in_deg=0

in_deg=0

in_deg=0

in_deg=1

in_deg=0 Queue

Sorted: c a b f

f

d

Topological Sorting Algorithm

Page 43: Lecture 9 Graph Traversal - KOCWcontents.kocw.net/KOCW/document/2015/sungkyunkwan/suhuiseon… · SWE2004: Principles in Programming | Spring 2014 | Euiseong Seo (euiseong@skku.edu)

43 SWE2004: Principles in Programming | Spring 2014 | Euiseong Seo ([email protected])

span d; store e in Queue.

c

a

b

e

d

f

in_deg=0

in_deg=0

in_deg=0

in_deg=0

in_deg=0

in_deg=0 Queue

Sorted: c a b f d

d

e

Topological Sorting Algorithm

Page 44: Lecture 9 Graph Traversal - KOCWcontents.kocw.net/KOCW/document/2015/sungkyunkwan/suhuiseon… · SWE2004: Principles in Programming | Spring 2014 | Euiseong Seo (euiseong@skku.edu)

44 SWE2004: Principles in Programming | Spring 2014 | Euiseong Seo ([email protected])

span e; Queue is empty

c

a

b

e

d

f

in_deg=0

in_deg=0

in_deg=0

in_deg=0

in_deg=0

in_deg=0 Queue

Sorted: c a b f d e

e

Topological Sorting Algorithm