0% found this document useful (0 votes)
80 views

Graph: Dr. Inayat-ur-Rehman COMSATS Institute of Information Technology, Islamabad

Dijkstra's algorithm finds the shortest path between a source node and all other nodes in a graph. It works by first selecting the node with the shortest distance from the source, adding it to the solution, and updating the distance of neighboring nodes. This process repeats, always selecting the unvisited node with the shortest distance, until all nodes have been visited.

Uploaded by

malik husnain
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
80 views

Graph: Dr. Inayat-ur-Rehman COMSATS Institute of Information Technology, Islamabad

Dijkstra's algorithm finds the shortest path between a source node and all other nodes in a graph. It works by first selecting the node with the shortest distance from the source, adding it to the solution, and updating the distance of neighboring nodes. This process repeats, always selecting the unvisited node with the shortest distance, until all nodes have been visited.

Uploaded by

malik husnain
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 39

Graph

Dr. Inayat-ur-Rehman
COMSATS Institute of Information Technology,
Islamabad
What is a Graph?

A graph G = (V,E) is composed of:


V: set of vertices
E: set of edges connecting the vertices in V
An edge e = (u,v) is a pair of vertices
Example:

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

networks (roads, flights,


communications)
Abbott

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

The degree of a vertex is the number of edges


incident to that vertex
For directed graph,
the in-degree of a vertex v is the number of edges
that have v as the head
the out-degree of a vertex v is the number of edges
that have v as the tail
Examples
0
3 2
0 1 2
3 3
3 1 2 3 3 4 5 6
3G1 1 1 G2 1 1
3
0 in:1, out: 1
directed graph
in-degree
out-degree 1 in: 1, out: 2

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

Directional path Bi-directional paths


More Terminology
simple path: no repeated vertices
a b

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

•connected graph: any two vertices are connected by some path

connected not connected


subgraph: subset of vertices and edges forming a graph
connected component: maximal connected subgraph. E.g., the graph
below has 3 connected components.
More…

tree - connected graph without cycles


forest - collection of trees

tree

tree

forest

tree

tree
More Connectivity

n = #vertices
n5
m = #edges m4
For a tree m = n - 1
If m < n - 1, G is
not connected

n5
m3
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

Let G=(V,E) be a graph with n vertices.


The adjacency matrix of G is a two-dimensional
n by n array, say adj_mat
If the edge (vi, vj) is in E(G), adj_mat[i][j]=1
If there is no such edge in E(G), adj_mat[i][j]=0
The adjacency matrix for an undirected graph is
symmetric; the adjacency matrix for a digraph
need not be symmetric
Examples for 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

We wish to find the shortest route between


Islamabad and A bbottabad. Given a road map with
all the possible routes how can we determine our
shortest route?

We could try to enumerate all possible routes.


Modeling Problem
We can model this problem with a
directed graph. Intersections correspond
to vertices, roads between intersections
correspond to edges and distance
corresponds to weights. One way roads
correspond to the direction of the edge.

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

Dijkstra’s algorithm does NOT allow


negative edges.
Dijkstra’s shortest path algorithm

Dijkstra’s algorithm solves the single source


shortest path problem in 2 stages.

Stage1: A greedy algorithm computes the shortest


distance from s to all other nodes in the graph
and saves a data structure.

Stage2 : Uses the data structure for finding a


shortest path from s to t.
Main idea

Assume that the shortest distances from the


starting node s to the rest of the nodes are
d(s, s)  d(s, s1)  d(s, s2)  …  d(s, sn-1)

In this case a shortest path from s to si may


include any of the vertices {s1, s2 … si-1} but
cannot include any sj where j > i.

Dijkstra’s main idea is to select the nodes and


compute the shortest distances in the order s,
s1, s2 ,…, sn-1
Example
s4

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

Assume s1, s2 … si-1 have been selected, and their


shortest distances have been stored in Solution

Select node si and save d(s, si) if si has the shortest


distance from s on a path that may include only s1, s2 …
si-1 as intermediate nodes. We call such paths special
To apply this selection rule efficiently, we need to
maintain for each unselected node v the distance of
the shortest special path from s to v, D[v].
Application Example
Solution = {(s, 0)}
D[s1]=5 for path [s, s1]
s4
D[s2]=  for path [s, s2]
D[s3]=10 for path [s, s3]
D[s4]=15 for path [s, s4]. 15 8
s3
Solution = {(s, 0), (s1, 5) } 2
D[s2]= 6 for path [s, s1, s2] 10
D[s3]=9 for path [s, s1, s3]
s
3 4 s2
D[s4]=15 for path [s, s4]
5
1
Solution = {(s, 0), (s1, 5), (s2, 6) } s1
D[s3]=8 for path [s, s1, s2, s3]
D[s4]=15 for path [s, s4]

Solution = {(s, 0), (s , 5), (s , 6),(s , 8), (s , 15) }


Graph Traversal
Problem: Search for a certain node or traverse
all nodes in the graph
Depth First Search
Once a possible path is found, continue the search
until the end of the path
Breadth First Search
Start several paths at a time, and advance in each
one step at a time
Depth-First Search
A B C D

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

Algorithm DFS(v); Input: A vertex v in a graph


Output: A labeling of the edges as “discovery” edges and “backedges”
for each edge e incident on v do
if edge e is unexplored then let w be the other endpoint of e
if vertex w is unexplored then label e as a discovery edge
recursively call DFS(w)
else label e as a backedge
Breadth-First Search

Like DFS, a Breadth-First Search (BFS) traverses a connected


component of a graph, and in doing so defines a spanning tree with
several useful properties.
The starting vertex s has level 0, and, as in DFS, defines that point as an
“anchor.”
In the first round, the string is unrolled the length of one edge, and all of
the edges that are only one edge away from the anchor are visited.
These edges are placed into level 1
In the second round, all the new edges that can be reached by unrolling
the string 2 edges are visited and placed in level 2.
This continues until every vertex has been assigned a level.
The label of any vertex v corresponds to the length of the shortest path
from s to v.

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

Algorithm BFS(s): Input: A vertex s in a graph


Output: A labeling of the edges as “discovery” edges and “cross edges”
initialize container L0 to contain vertex s
i0
while Li is not empty do
create container Li+1 to initially be empty
for each vertex v in Li do
if edge e incident on v do
let w be the other endpoint of e
if vertex w is unexplored then
label e as a discovery edge
insert w into Li+1
else label e as a cross edge
ii+1

35
Applications: Finding a Path

Find path from source vertex s to destination


vertex d
Use graph search starting at s and terminating
as soon as we reach d
Need to remember edges traversed
Use depth – first search ?
Use breath – first search?
DFS vs. BFS

F B A start
DFS Process E

G D C
destination

C DFS on C D Call DFS on D


B DFS on B B B Return to call on B
A DFS on A A A A

G Call DFS on G found destination - done!


D Path is implicitly stored in DFS recursion
B Path is: A, B, D, G
A
DFS vs. BFS

F B A start
E
BFS Process
G D C
destination

rear front rear front rear front rear front

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

G found destination - done!


Dequeue D Path must be stored separately
Add G
Reference
Rada Mihalcea
John Lewis and William Loftus
Data structure through C in depth
Data Structure through C

You might also like