Graph: Dr. Inayat-ur-Rehman COMSATS Institute of Information Technology, Islamabad
Graph: Dr. Inayat-ur-Rehman COMSATS Institute of Information Technology, Islamabad
Dr. Inayat-ur-Rehman
COMSATS Institute of Information Technology,
Islamabad
What is a Graph?
a b V= {a,b,c,d,e}
E= {(a,b),(a,c),
c (a,d),
(b,e),(c,d),(c,e),
(d,e)}
d e
Applications
CS16
electronic circuits
LAX
Islamabad Haripur
Lahore
Taxila
khanpur
Terminology:
Adjacent and Incident
If (v0, v1) is an edge in an undirected graph,
v0 and v1 are adjacent
The edge (v0, v1) is incident on vertices v0 and v1
If <v0, v1> is an edge in a directed graph
v0 is adjacent to v1, and v1 is adjacent from v0
The edge <v0, v1> is incident on v0 and v1
Terminology:
Degree of a Vertex
2 in: 1, out: 0
G3
Terminology:
Path
path: sequence of
3 2
vertices v1,v2,. . .vk such
that consecutive
vertices vi and vi+1 are 3
adjacent.
3 3
a b a b
c c
d e d e
abedc bedc
7
Graphs: Example
1
6
2 3 7
4 5
8
Legend
bec
c
d e
cycle: simple path, except that the last vertex is the same as
the first vertex
a b
acda
c
d e
Even More Terminology
tree
tree
forest
tree
tree
More Connectivity
n = #vertices
n5
m = #edges m4
For a tree m = n - 1
If m < n - 1, G is
not connected
n5
m3
Oriented (Directed) Graph
A graph where edges are directed
Directed vs. Undirected Graph
An undirected graph is one in which the pair
of vertices in a edge is unordered, (v0, v1) =
(v1,v0)
A directed graph is one in which each edge is
a directed pair of vertices, <v0, v1> != <v1,v0>
tail head
Graph Representations
Adjacency Matrix
Adjacency Lists
Adjacency Matrix
0
0
1 2
3 1
0 1 1 1 0 1 0
1 0 1 1
1 0 1
1 1 0 1 2 0 0 0
1 1 1 0
G2
G1
0 0 4
2 1 5
1 2 3 6
3 7
0 1 2 3 0 1 2
1 0 2 3 1 0 3
2 0 1 3 2 0 3
3 0 1 2 3 1 2
G1 0 4 5
5 4 6
0 1 6 5 7
1 0 2 1
7 6
2
G3 G4
2
An undirected graph with n vertices and e edges ==> n head nodes and 2e list nodes
Single-Source Shortest Paths
The problem:
Given a weighted digraph and a vertex s in the
graph: find a shortest path from s to t
The distance of a shortest path
Case: The graph may have negative edges but no
negative cycles. The shortest distance from s
to t can be computed.
1 8
s A B t d(s,t)=6
-3
Shortest Path Algorithms
d(s, s) =0
8
d(s, s1)=5 15
s3
d(s, s2)=6 2
d(s, s3)=8 10
s
3 4 s2
d(s, s4)=15
5
1
Note: The shortest path s1
from s to s2 includes s1 as
an intermediate node but
cannot include s3 or s4.
Dijkstra’s greedy selection rule
E F G H
I J K L
M N O P
Exploring a Labyrinth
Without Getting Lost
A depth-first search (DFS) in an undirected graph G is like
wandering in a labyrinth with a string and a can of red paint
without getting lost.
We start at vertex s, tying the end of our string to the point and
painting s “visited”. Next we label s as our current vertex called
u.
Now we travel along an arbitrary edge (u, v).
If edge (u, v) leads us to an already visited vertex v we return to
u.
If vertex v is unvisited, we unroll our string and move to v, paint
v “visited”, set v as our current vertex, and repeat the previous
steps.
Depth-First Search
32
BFS - A Graphical
Representation
0 0 1
A B C D A B C D
a) E F G H E F G H b)
I J K L I J K L
M N O P M N O P
0 1 2
0 1 2 3
A B C D B C D
A
c) d)
E F G H E F G H
I J K L
I J K L
M N O P
M N O P
33
More BFS
0 1 2 3 0 1 2 3
A B C D A B C D
E F G H E F G H
4 4
I J K L I J K L
M N O P M N O P 5
BFS Pseudo-Code
35
Applications: Finding a Path
F B A start
DFS Process E
G D C
destination
F B A start
E
BFS Process
G D C
destination
A B D C D
Initial call to BFS on A Dequeue A Dequeue B Dequeue C
Add A to queue Add B Add C, D Nothing to add
rear front