Graph and Traversal - PowerPoint PPT Presentation

About This Presentation
Title:

Graph and Traversal

Description:

Breadth-First Search Breadth-First Search Breadth ... Ford Algorithm Bellman-Ford Algorithm Detecting Cycles Idea: Use DFS Depth-First Search Depth ... – PowerPoint PPT presentation

Number of Views:76
Avg rating:3.0/5.0
Slides: 129
Provided by: NUS
Category:

less

Transcript and Presenter's Notes

Title: Graph and Traversal


1
Graph and Traversal
2
Graph
edge
vertex
3
Weighted Graph
1
3
0
5
-2
5
4
Undirected Graph
5
Complete Graph (Clique)
6
Path
a
Length 4
b
c
d
e
7
Cycle
a
g
b
c
f
d
e
8
Directed Acyclic Graph (DAG)
9
Degree
10
In-Degree Out-Degree
11
Disconnected Graph
12
Connected Components
13
Formally
  • A weighted graph G (V, E, w), where
  • V is the set of vertices
  • E is the set of edges
  • w is the weight function

14
Variations of (Simple) Graph
  • Multigraph
  • More than one edge between two vertices
  • E is a bag, not a set
  • Hypergraph
  • Edges consists of more than two vertex

15
Example
  • V a, b, c
  • E (a,b), (c,b), (a,c)
  • w ((a,b), 4), ((c, b), 1), ((a,c),-3)

a
4
-3
b
c
1
16
Adjacent Vertices
  • adj(v) set of vertices adjacent to v
  • adj(a) b, c
  • adj(b)
  • adj(c) b
  • ?v adj(v) E
  • adj(v) Neighbours of v

a
4
-3
b
c
1
17
Applications
18
Travel Planning
direct flight
city
5
cost
19
Question
  • What is the shortest way to travel between A and
    B?
  • SHORTEST PATH PROBLEM
  • How to mimimize the cost of visiting n cities
    such that we visit each city exactly once, and
    finishing at the city where we start from?
  • TRAVELING SALESMAN PROBLEM

20
Internet
network link
computer
21
Question
  • What is the shortest route to send a packet from
    A to B?
  • SHORTEST PATH PROBLEM

22
(No Transcript)
23
The Web
web link
web page
24
Question
  • Which web pages are important?
  • Which set of web pages are likely to be of the
    same topic?

25
Module Selection
prerequisite
module
26
Question
  • Find a sequence of modules to take that satisfy
    the prerequisite requirements.
  • TOPOLOGICAL SORT

27
SDU Matchmaking
compatible
participant
28
Questions
  • How to match-up as many pairs as possible?
  • MAXIMUM MATCHING PROBLEM

29
Terrorist
knows
suspects
30
Question
  • Who are the important figures in a terrorist
    network?

31
(No Transcript)
32
Other Applications
  • Biology
  • VLSI Layout
  • Vehicle Routing
  • Job Scheduling
  • Facility Location

33
Implementation
34
Adjacency Matrix
  • double vertex

1
1 2 3
1 ? 4 -3
2 ? ? ?
3 ? 1 ?
4
-3
2
3
1
35
Adjacency List
  • EdgeList vertex

1
2
3
3
-3
2
4
1
4
-3
2
3
2
1
1
neighbour
cost
36
Avoid Pointers in Competition..
1 2 3
2
3 2
1
4
-3
2
3
1 4 -3
2
3 1
1
37
Breadth-First Search
38
Breadth-First Search
A
C
B
D
E
F
39
Breadth-First Search
A
C
B
D
E
F
40
Breadth-First Search
A
C
B
D
E
F
41
Breadth-First Search
A
C
B
D
E
F
42
Breadth-First Search
A
C
B
D
E
F
43
Breadth-First Search
A
C
B
D
E
F
44
Breadth-First Search
A
C
B
D
E
F
45
Breadth-First Search
A
C
B
D
E
F
46
Breadth-First Search
0
1
2
2
2
3
47
Level-Order on Tree
  • if T is empty return
  • Q new Queue
  • Q.enq(T)
  • while Q is not empty
  • curr Q.deq()
  • print curr.element
  • if T.left is not empty
  • Q.enq(curr.left)
  • if curr.right is not empty
  • Q.enq(curr.right)

48
BFS(v)
  • Q new Queue
  • Q.enq (v)
  • while Q is not empty
  • curr Q.deq()
  • if curr is not visited
  • print curr
  • mark curr as visited
  • foreach w in adj(curr)
  • if w is not visited
  • Q.enq(w)

A
C
B
D
E
F
49
  • Q new Queue
  • Q.enq (v)
  • foreach w in V
  • set w.parent to null
  • while Q is not empty
  • curr Q.deq()
  • mark curr as visited
  • foreach w in adj(curr)
  • if w is not visited w.parent is null
  • w.parent curr
  • Q.enq(w)

A
C
B
D
E
F
50
Calculating Level
  • Q new Queue
  • Q.enq (v)
  • v.level 0
  • while Q is not empty
  • curr Q.deq()
  • if curr is not visited
  • mark curr as visited
  • foreach w in adj(curr)
  • if w is not visited
  • w.level curr.level 1
  • Q.enq(w)

A
C
B
D
E
F
51
Search All Vertices
  • Search(G)
  • foreach vertex v
  • mark v as unvisited
  • foreach vertex v
  • if v is not visited
  • BFS(v)

52
Running Time
  • Q new Queue
  • Q.enq (v)
  • while Q is not empty
  • curr Q.deq()
  • if curr is not visited
  • print curr
  • mark curr as visited
  • foreach w in adj(curr)
  • if w is not visited
  • Q.enq(w)

Main Loop
Initialization
Total Running Time
53
Depth-First Search
54
Depth-First Search
A
C
B
D
E
F
55
Depth-First Search
A
C
B
D
E
F
56
Depth-First Search
A
C
B
D
E
F
57
Depth-First Search
A
C
B
D
E
F
58
Depth-First Search
A
C
B
D
E
F
59
Depth-First Search
A
C
B
D
E
F
60
Depth-First Search
A
C
B
D
E
F
61
Depth-First Search
A
C
B
D
E
F
62
DFS(v)
  • S new Stack
  • S.push (v)
  • mark v as visited
  • while S is not empty
  • curr S.top()
  • if every vertex in adj(curr)
  • is visited
  • S.pop()
  • else
  • let w be an unvisited vertex in adj(curr)
  • S.push(w)
  • print and mark w as visited

A
C
B
D
E
F
63
Recursive Version DFS(v)
  • print v
  • marked v as visited
  • foreach w in adj(v)
  • if w is not visited
  • DFS(w)

A
C
B
D
E
F
64
Search All Vertices
  • Search(G)
  • foreach vertex v
  • mark v as unvisited
  • foreach vertex v
  • if v is not visited
  • DFS(v)

65
Running Time
  • DFS ?(V E)

66
Single-SourceShortest Path
67
Applications
  • BFS
  • shortest path
  • DFS
  • longest path in DAG
  • finding connected component
  • detecting cycles
  • topological sort

68
Definition
  • A path on a graph G is a sequence of vertices
    v0, v1, v2, .. vn where (vi,vi1)?E
  • The cost of a path is the sum of the cost of all
    edges in the path.

69
Unweighted Shortest Path
A
C
B
D
E
F
70
ShortestPath(s)
  • Run BFS(s)
  • w.level shortest distance from s
  • w.parent shortest path from s

71
Positive Weighted Shortest Path
A
5
1
3
C
B
5
1
3
1
D
E
F
4
2
72
BFS(s) does not work
  • Must keep track of smallest distance so far.
  • If we found a new, shorter path, update the
    distance.

73
Idea 1
s
w
10
8
2
6
v
74
Idea 2
w
6
6
v
6
6
6
6
6
75
Definition
  • distance(v) shortest distance so far from s to
    v
  • parent(v) previous node on the shortest path so
    far from s to v
  • cost(u, v) the cost of edge from u to v

76
Example
s
w
10
8
2
6
distance(w) 8 cost(v,w) 2 parent(w) v
v
77
Relax(v,w)
  • d distance(v) cost(v,w)
  • if distance(w) gt d then
  • distance(w) d
  • parent(w) v

s
w
10
8
2
6
v
78
Dijkstras Algorithm
A
5
1
3
C
B
5
1
3
1
D
E
F
4
2
79
Dijkstras Algorithm
0
5
1
3
8
8
5
1
3
1
8
8
8
4
2
80
Dijkstras Algorithm
0
5
1
3
5
8
5
1
3
1
8
8
8
4
2
81
Dijkstras Algorithm
0
5
1
3
5
8
5
1
3
1
8
8
8
4
2
82
Dijkstras Algorithm
0
5
1
3
5
8
5
1
3
1
10
6
8
4
2
83
Dijkstras Algorithm
0
5
1
3
5
8
5
1
3
1
10
6
8
4
2
84
Dijkstras Algorithm
0
5
1
3
5
8
5
1
3
1
8
6
10
4
2
85
Dijkstras Algorithm
0
5
1
3
5
8
5
1
3
1
8
6
10
4
2
86
Dijkstras Algorithm
0
5
1
3
5
8
5
1
3
1
8
6
10
4
2
87
Dijkstras Algorithm
0
5
1
3
5
8
5
1
3
1
8
6
10
4
2
88
Dijkstras Algorithm
0
5
3
5
8
1
8
6
10
4
2
89
Dijkstras Algorithm
  • color all vertices yellow
  • foreach vertex w
  • distance(w) INFINITY
  • distance(s) 0

90
Dijkstras Algorithm
  • while there are yellow vertices
  • v yellow vertex with min distance(v)
  • color v red
  • foreach neighbour w of v
  • relax(v,w)

91
Running Time
O(V2 E)
  • color all vertices yellow
  • foreach vertex w
  • distance(w) INFINITY
  • distance(s) 0
  • while there are yellow vertices
  • v yellow vertex with min distance(v)
  • color v red
  • foreach neighbour w of v
  • relax(v,w)

92
Using Priority Queue
  • foreach vertex w
  • distance(w) INFINITY
  • distance(s) 0
  • pq new PriorityQueue(V)
  • while pq is not empty
  • v pq.deleteMin()
  • foreach neighbour w of v
  • relax(v,w)

93
Initialization O(V)
  • foreach vertex w
  • distance(w) INFINITY
  • distance(s) 0
  • pq new PriorityQueue(V)

94
Main Loop
  • while pq is not empty
  • v pq.deleteMin()
  • foreach neighbour w of v
  • relax(v,w)

95
Main Loop
O((VE) log V)
  • while pq is not empty
  • v pq.deleteMin()
  • foreach neighbour w of v
  • d distance(v) cost(v,w)
  • if distance(w) gt d then
  • // distance(w) d
  • pq.decreaseKey(w, d)
  • parent(w) v

96
cost(u,v) lt 0?
A
-5
1
3
C
B
5
1
3
1
D
E
F
-4
2
97
Problem 1
w
6
10
v
6
-5
6
6
6
6
98
Problem 2
A
-5
1
3
C
B
5
1
3
1
D
E
F
-4
2
99
Basic Idea
  • Longest possible path between two vertices V-1

100
Basic Idea
  • foreach edge (u,v)
  • relax(u , v)
  • We will get the shortest paths of length 1
    between s and all other vertices.
  • Repeat the above pseudocode V-1 times.

101
Bellman-Ford Algorithm
0
5
1
-3
8
8
3
1
3
1
8
8
8
-4
-2
102
Bellman-Ford Algorithm
0
5
1
-3
5
8
3
1
3
1
8
8
8
-4
-2
103
Bellman-Ford Algorithm
0
5
1
-3
5
2
3
1
3
1
8
6
8
-4
-2
104
Bellman-Ford Algorithm
0
5
1
-3
5
2
3
1
3
1
4
3
2
-4
-2
105
Bellman-Ford Algorithm
0
5
1
-3
5
2
3
1
3
1
1
3
-1
-4
-2
106
Bellman-Ford Algorithm
  • do V-1 times
  • foreach edge (u,v)
  • relax(u,v)
  • // check for negative weight cycle
  • foreach edge (u,v)
  • if distance(v) gt distance(u) cost(u,v)
  • ERROR has negative cycle

107
Detecting Cycles
108
Idea Use DFS
  • Denote vertex as visited, unvisited and
    visiting
  • Mark a vertex as visited vertex is finished

109
Depth-First Search
unvisited
A
visiting
visited
C
B
D
E
F
110
Depth-First Search
unvisited
A
visiting
visited
C
B
D
E
F
111
Depth-First Search
unvisited
A
visiting
visited
C
B
D
E
F
112
Depth-First Search
unvisited
A
visiting
visited
C
B
D
E
F
113
Depth-First Search
unvisited
A
visiting
visited
C
B
D
E
F
114
Depth-First Search
unvisited
A
visiting
visited
C
B
D
E
F
115
Depth-First Search
unvisited
A
visiting
visited
C
B
D
E
F
116
Depth-First Search
unvisited
A
visiting
visited
C
B
D
E
F
117
Aha! Cycles!
unvisited
A
visiting
visited
C
B
D
E
F
118
Type of Edges
unvisited
Tree Edge
visiting
visited
Backward Edge
Forward Edge
Cross Edge
119
Longest Path
  • (in a DAG)

120
Counting Length of the Path
A
C
B
D
E
F
1
0
121
Counting Length of the Path
unvisited
A
visiting
visited
C
B
D
E
F
2
1
0
122
Counting Length of the Path
unvisited
A
visiting
visited
3
C
B
D
E
F
2
1
0
123
Counting Length of the Path
unvisited
A
visiting
5
visited
3
4
C
B
D
E
F
2
1
0
124
Longest Path
  • Run DFS as usual
  • When a vertex v is finished, set
  • L(v) max L(u) 1 for all edge (v,u)

125
Topological Sort
126
Topological Sort
  • Goal Order the vertices, such that if there is a
    path from u to v, u appears before v in the
    output.

127
Topological Sort
  • ACBEFD
  • ACBEDF
  • ACDBEF

A
C
B
D
E
F
128
Topological Sort
  • Run DFS as usual
  • When a vertex is finish, push it onto a stack
Write a Comment
User Comments (0)
About PowerShow.com