Dagajal 150302145746 Conversion Gate02
Dagajal 150302145746 Conversion Gate02
5 0
-2
5
Undirected Graph
Complete Graph (Clique)
Path
Length = 4 a
d e
Cycle
a
b g
c f
d e
Directed Acyclic Graph (DAG)
Degree
In-Degree Out-Degree
Disconnected Graph
Connected Components
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
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
Example
V = { a, b, c }
E = { (a,b), (c,b), (a,c) }
w = { ((a,b), 4), ((c, b), 1), ((a,c),-3) }
a
-3 4
c b
1
Adjacent Vertices
adj(v) = set of vertices adjacent to v
adj(a) = {b, c}
adj(b) = {}
a
adj(c) = {b}
-3 4
∑ |adj(v)| = |E|
v
c
1
b
adj(v): Neighbours of v
direct
city flight
cost
Question
What is the shortest way to travel between A and
B?
“SHORTEST PATH PROBLEM”
“TOPOLOGICAL SORT”
Other Applications
Biology
VLSI Layout
Vehicle Routing
Job Scheduling
Facility Location
:
:
Adjacency Matrix
double vertex[][];
1
1 2 3 -3 4
1 ∞ 4 -3 3 2
1
2 ∞ ∞ ∞
3 ∞ 1 ∞
Adjacency List
EdgeList vertex[];
1 3 -3 2 4
1
2 -3 4
3 2
3 2 1 1
neighbour cost
“Avoid Pointers in Competition..”
1 2 3
2 1
3 2 -3 4
3 2
1 4 -3 1
2
3 1
A
B C
F E D
A
B C
F E D
A
B C
F E D
A
B C
F E D
A
B C
F E D
A
B C
F E D
A
B C
F E D
A
B C
F E D
0
2 1
3 2 2
Level-Order on Tree
if T is empty return 1
Q = new Queue
2 3
Q.enq(T)
while Q is not empty 4 5 6 7
curr = Q.deq()
print curr.element 8 9 0
B C
F E D
A
B C
F E D
A
B C
F E D
Applications
BFS
shortest path
DFS
longest path in DAG
finding connected component
detecting cycles
topological sort
Definition
A path on a graph G is a sequence of vertices v0, v1,
v2, .. vn where (vi,vi+1)∈E
B C
F E D
A
B C
F E D
ShortestPath(s)
Run BFS(s)
w.level: shortest distance from s
w.parent: shortest path from s
A
1 5
3
B C
3 1 5
1
F E D
4 2
BFS(s) does not work
Must keep track of smallest distance so far.
2
6
v
Definition
distance(v) : shortest distance so far from s to v
2
6
distance(w) = 8
v
cost(v,w) = 2
parent(w) = v
A
1 5
3
B C
3 1 5
1
F E D
4 2
0
1 5
8 3
8
3 1 5
1
8
8
4 2
0
1 5
3
5
8
3 1 5
1
8
8
4 2
0
1 5
3
5
8
3 1 5
1
8
8
4 2
0
1 5
3
8 5
3 1 5
1
6 10
8
4 2
0
1 5
3
8 5
3 1 5
1
6 10
8
4 2
0
1 5
3
8 5
3 1 5
1
10 6 8
4 2
0
1 5
3
8 5
3 1 5
1
10 6 8
4 2
0
1 5
3
8 5
3 1 5
1
10 6 8
4 2
0
1 5
3
8 5
3 1 5
1
10 6 8
4 2
0
5
3
8 5
1
10 6 8
4 2
Dijkstra’s Algorithm
color all vertices yellow
foreach vertex w
distance(w) = INFINITY
distance(s) = 0
Dijkstra’s Algorithm
while there are yellow vertices
v = yellow vertex with min distance(v)
color v red
foreach neighbour w of v
relax(v,w)
Using Priority Queue
foreach vertex w
distance(w) = INFINITY
distance(s) = 0
pq = new PriorityQueue(V)
8 -3
8
3 1 3
1
8
8
-4 -2
0
1 5
-3
5
8
3 1 3
1
8
8
-4 -2
0
1 5
-3
2 5
3 1 3
1
6 8
8
-4 -2
0
1 5
-3
2 5
3 1 3
1
2 3 4
-4 -2
0
1 5
-3
2 5
3 1 3
1
-1 3 1
-4 -2
Bellman-Ford Algorithm
do |V|-1 times
foreach edge (u,v)
relax(u,v)
visiting A
visited
B C
F E D
unvisited
visiting A
visited
B C
F E D
unvisited
visiting A
visited
B C
F E D
unvisited
visiting A
visited
B C
F E D
unvisited
visiting A
visited
B C
F E D
unvisited
visiting A
visited
B C
F E D
unvisited
visiting A
visited
B C
F E D
unvisited
visiting A
visited
B C
F E D
unvisited
visiting A
visited
B C
F E D
unvisited
Tree Edge
visiting
visited
Backward Edge
Forward Edge
Cross Edge
(in a DAG)
A
B C
F E D
1 0
unvisited
visiting A
visited
B C
F E D
2 1 0
unvisited
visiting A
visited
3
B C
F E D
2 1 0
unvisited
visiting 5
A
visited
3 4
B C
F E D
2 1 0
Longest Path
Run DFS as usual
When a vertex v is finished, set
L(v) = max {L(u) + 1} for all edge (v,u)
Topological Sort
Goal: Order the vertices, such that if there is a path
from u to v, u appears before v in the output.
Topological Sort
ACBEFD A
ACBEDF B C
ACDBEF
F E D
Topological Sort
Run DFS as usual
When a vertex is finish, push it onto a stack