1 SWE2004: Principles in Programming | Spring 2014 | Euiseong Seo ([email protected])
Lecture 9 Graph Traversal
Euiseong Seo
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?
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
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
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
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
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
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
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
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
11 SWE2004: Principles in Programming | Spring 2014 | Euiseong Seo ([email protected])
Tree Traversal
BFS (Breadth First Search)
DFS (Depth First Search)
12 SWE2004: Principles in Programming | Spring 2014 | Euiseong Seo ([email protected])
BFS
s
2
5
4
7
8
3 6 9
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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!
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
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
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
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
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
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
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
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
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
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
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
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