0% found this document useful (0 votes)
444 views57 pages

Data Structures and Algorithm Unit 5

This document defines and explains key concepts related to graphs: - A graph consists of vertices and edges connecting the vertices. It can be directed or undirected. - Common graph terminology includes adjacent/incident vertices and edges, paths, cycles, subgraphs, connected components, and degrees of vertices. - Graphs can be represented using adjacency matrices or adjacency lists and traversed using depth-first search or breadth-first search algorithms. - Common graph algorithms include finding the shortest path between vertices using Dijkstra's algorithm.

Uploaded by

Akhil Singh
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
Download as ppt, pdf, or txt
0% found this document useful (0 votes)
444 views57 pages

Data Structures and Algorithm Unit 5

This document defines and explains key concepts related to graphs: - A graph consists of vertices and edges connecting the vertices. It can be directed or undirected. - Common graph terminology includes adjacent/incident vertices and edges, paths, cycles, subgraphs, connected components, and degrees of vertices. - Graphs can be represented using adjacency matrices or adjacency lists and traversed using depth-first search or breadth-first search algorithms. - Common graph algorithms include finding the shortest path between vertices using Dijkstra's algorithm.

Uploaded by

Akhil Singh
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1/ 57

Graphs

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
c
d
e
V= {a,b,c,d,e}

E= {(a,b),(a,c),(a,d),
(b,e),(c,d),(c,e),
(d,e)}
Applications
electronic circuits



networks (roads, flights,
communications)

CS16
LAX
JFK
LAX
DFW
STL
HNL
FTL
Terminology
Adjacent and Incident
If (v0, v1) is an edge in an undirected graph,
v0 and v1 are adjacent (w.r.t.vertices)
The edge (v0, v1) is incident on vertices v0
and v1 (w.r.t.edges)
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

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
if di is the degree of a vertex i in a graph G with n vertices and e
edges, the number of edges is
Degree of a Vertex
e d
i
n
=

( ) /
0
1
2
0
1 2
3 4 5 6
G1
G2
3
2
3 3
1
1
1
1
directed graph
in-degree
out-degree
0
1
2
G3
in:1, out: 1
in: 1, out: 2
in: 1, out: 0
0
1 2
3
3
3
3
Examples
7
Path
path: sequence of
vertices v
1
,v
2
,. . .v
k

such that
consecutive vertices
v
i
and v
i+1
are
adjacent.
3
3
3
3
2
a
b
c
d
e
a b
c
d
e
a b e d c b e d c
More Terminology
simple path: no repeated vertices





cycle: simple path, except that the last vertex is the
same as the first vertex

a b
c
d
e
b e c
Even More Terminology
subgraph: subset of vertices and edges forming a graph
connected component: maximal connected subgraph. E.g.,
the graph below has 3 connected components.
connected not connected
connected graph: any two vertices are connected by some path

0 0
1 2 3
1 2 0
1 2
3
(i) (ii) (iii) (iv)
(a) Some of the subgraph of G
1

0
0
1
0
1
2
0
1
2
(i) (ii) (iii) (iv)
(b) Some of the subgraph of G
3

0
1 2
3
G1
0
1
2
G3
Subgraphs Examples
Connectivity
Let n = #vertices, and m = #edges
A complete graph: one in which all pairs of vertices are adjacent
How many total edges in a complete graph?
Each of the n vertices is incident to n-1 edges, however, we
would have counted each edge twice! Therefore, intuitively, m
= n(n -1)/2.
Therefore, if a graph is not complete, m < n(n -1)/2

n = 5
m = (5
-
4)/2 = 10
More Connectivity
n = #vertices
m = #edges
For a tree m = n - 1


n = 5
m = 4
n = 5
m = 3
If m < n - 1, G is
not connected
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
1
1
1
1
0
1
1
1
1
0
1
1
1
1
0

(
(
(
(
0
1
0
1
0
0
0
1
0

(
(
(
0
1
1
0
0
0
0
0
1
0
0
1
0
0
0
0
1
0
0
1
0
0
0
0
0
1
1
0
0
0
0
0
0
0
0
0
0
1
0
0
0
0
0
0
1
0
1
0
0
0
0
0
0
1
0
1
0
0
0
0
0
0
1
0

(
(
(
(
(
(
(
(
(
(
(
G1
G2
G4
0
1 2
3
0
1
2
1
0
2
3
4
5
6
7
symmetric
undirected: n
2
/2
directed: n
2

Merits of Adjacency Matrix
From the adjacency matrix, to determine
the connection of vertices is easy
The degree of a vertex is
For a digraph (= directed graph), the row
sum is the out_degree, while the column
sum is the in_degree
adj mat i j
j
n
_ [ ][ ]
=

0
1
ind vi A j i
j
n
( ) [ , ] =
=

0
1
outd vi A i j
j
n
( ) [ , ] =
=

0
1
Adjacency Lists (data structures)
#define MAX_VERTICES 50
typedef struct node *node_pointer;
typedef struct node {
int vertex;
struct node *link;
};
node_pointer graph[MAX_VERTICES];

Each row in adjacency matrix is represented as an adjacency list.
0
1
2
3
0
1
2
0
1
2
3
4
5
6
7
1 2 3
0 2 3
0 1 3
0
1 2
G1
1
0 2
G3
1 2
0 3
0 3
1 2
5
4
6
5 7
6
G4
0
1 2
3
0
1
2
1
0
2
3
4
5
6
7
An undirected graph with n vertices and e edges ==> n head nodes and 2e list nodes
Graph Traversal
Problem: Search for a certain node or
traverse all nodes in the graph, find a path
from one node to another
Depth First Search
Once a possible path is found, continue the
search until the end of the path
Breadth First Search
Depth First Search
Generalized version of pre-order traversal
Starting @ a vertex v, we process v and
then recursively traverse all vertices
adjacent to v
Be careful about cycles !!
Mark a discovered vertex as visited and
perform DFS on vertices that are not already
visited
Backtracking !! what DS comes to your
mind ?

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
Recursive call
Void DFS(Vertex v){
Visited[v] = true;
For each W adjacent to V
if(!Visited[W])
DFS (W)
}

What should be the input and how will you use
this ?

Breadth First Search
Start several paths at a time, and
advance in each one step at a time
Many simultaneous explorations starting
a common point and spreading out
independently What traversal comes to
your mind ??
There is no backtracking


26
Breadth-First Search

The starting vertex s has level 0,
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.
27
BFS - A Graphical
Representation
M N O P
I J K L
E F G H
A B C D
0
M N O P
I J K L
E F G H
A B C D
0 1
M N O P
I J K L
E F G H
A C D B
0 1 2
M N O P
I J K L
E F G H
A B C D
0 1 2 3
d)
c)
b) a)
More BFS
M N O P
I J K L
E F G H
A B C D
4
0 1 2 3
M N O P
I J K L
E F G H
A B C D
4
5
0 1 2 3
29
BFS Pseudo-Code
Algorithm BFS(s): Input: A graph represented by adjacency list structure ; vertex s in
exists in V
Output: A BFT Breadth First Tree stored in parent array
void breadthFirstSearch(int List [] adjVertices,int n, int s,int parent [])
Int color [n]
Initialize color[1]color[n] to white // Undiscovered Vetices
Parent[s] = -1 // Starting node has no parent
Color[s] = gray
Enqueue(pending,s) //pendind is the Queue maintained to hold discovered vertices
While(pending is non-empty)
v = front(pending)
Dequeue(pending)
for each vertex w adjacent to v:
if(color[w] == white)
color[w] = gray //discovered vertices
enqueue(pending,w)
parent[w] = v
//End of for
color[v] = black //processed vertices
Return



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
DFS vs. BFS
E
F
G
B
C
D
A
start
destination
A
DFS on A
A
DFS on B
B
A
DFS on C
B
C
A
B
Return to call on B
D
Call DFS on D
A
B
D
Call DFS on G
G
found destination - done!
Path is implicitly stored in DFS recursion
Path is: A, B, D, G
DFS Process
DFS vs. BFS
E
F
G
B
C
D
A
start
destination
BFS Process
A
Initial call to BFS on A
Add A to queue
B
Dequeue A
Add B
front rear front rear
C
Dequeue B
Add C, D
front rear
D D
Dequeue C
Nothing to add
front rear
G
Dequeue D
Add G
front rear
found destination - done!
Path must be stored separately
Greedy Algorithms
Greedy algorithms work in phases.
In each phase, a decision is made that
appears to be good, without regard for future
consequences.
Take what you can get now
When the algorithm terminates, we hope that
the local optimum is equal to the global
optimum.
Dijkstras algorithm finds the shortest path from
a source vertex to all other vertices in a
weighted directed graph without negative edge
weights.

Dijkstras Algorithm
For a Graph G
Vertices V = {v
1
, v
n
}
And edge weights w
ij
, for edge
connecting v
i
to v
j
.
Let the source be v
1.


Initialize a Set S = C.
Keep track of the vertices for
which weve already
computed their shortest path
from the source.

Initialize an array D of
estimates of shortest
distances.
Initialize D[source]=0,
everything else D[i] = .
This says our estimate from
the source to the source is 0,
everything else is initially.
While S!=V (or while
we havent finished all
vertices):
1) Find the vertex with
the minimum dist (not
already in S).
2) Add this vertex, v
i
to S
3) Recompute all
estimates based on v
i


Specifically compute
D[i]+w
ij.
If this < D[j] then
set D[j] = D[i]+w
ij
Dijkstras Algorithm
Dijkstras
algorithm relies
on:
Knowing that all
shortest paths
contain subpaths
that are also
shortest paths.
c
b a d
10 2
4 8
The shortest path
from a to b is 10,
so the shortest path
from a to d has to go
through b
so it will also contain
the shortest path from
a to b which is 10,
plus the additional 2 =
12.
25
A
H
B
F
E
D
C
G
Walk-Through
9
7
2
10
18
3
4
3
7
5
8
9
4
3
10
Initialize array
K d
v
p
v
A F
B F
C F
D F
E F
F F
G F
H F
2
25
A
H
B
F
E
D
C
G
9
7
2
10
18
3
4
3
7
5
8
9
4
3
10
Start with G
K d
v
p
v
A
B
C
D
E
F
G T 0
H
2
25
A
H
B
F
E
D
C
G
9
7
2
10
18
3
4
3
7
5
8
9
4
3
10
Update unselected nodes
K d
v
p
v
A
B
C
D 2 G
E
F
G T 0
H 3 G
2
25
A
H
B
F
E
D
C
G
9
7
2
10
18
3
4
3
7
5
8
9
4
3
10
Select minimum distance
K d
v
p
v
A
B
C
D T 2 G
E
F
G T 0
H 3 G
2
25
A
H
B
F
E
D
C
G
9
7
2
10
18
3
4
3
7
5
8
9
4
3
10
Update unselected nodes
K d
v
p
v
A
B
C
D T 2 G
E 27 D
F 20 D
G T 0
H 3 G
2
25
A
H
B
F
E
D
C
G
9
7
2
10
18
3
4
3
7
5
8
9
4
3
10
Select minimum distance
K d
v
p
v
A
B
C
D T 2 G
E 27 D
F 20 D
G T 0
H T 3 G
2
25
A
H
B
F
E
D
C
G
9
7
2
10
18
3
4
3
7
5
8
9
4
3
10
Update unselected nodes
K d
v
p
v
A 7 H
B 12 H
C
D T 2 G
E 27 D
F 20 D
G T 0
H T 3 G
2
25
A
H
B
F
E
D
C
G
9
7
2
10
18
3
4
3
7
5
8
9
4
3
10
Select minimum distance
K d
v
p
v
A T 7 H
B 12 H
C
D T 2 G
E 27 D
F 20 D
G T 0
H T 3 G
2
25
A
H
B
F
E
D
C
G
9
7
2
10
18
3
4
3
7
5
8
9
4
3
10
Update unselected nodes
K d
v
p
v
A T 7 H
B 12 H
C
D T 2 G
E 27 D
F 17 A
G T 0
H T 3 G
2
25
A
H
B
F
E
D
C
G
9
7
2
10
18
3
4
3
7
5
8
9
4
3
10
Select minimum distance
K d
v
p
v
A T 7 H
B T 12 H
C
D T 2 G
E 27 D
F 17 A
G T 0
H T 3 G
2
25
A
H
B
F
E
D
C
G
9
7
2
10
18
3
4
3
7
5
8
9
4
3
10
Update unselected nodes
K d
v
p
v
A T 7 H
B T 12 H
C 16 B
D T 2 G
E 22 B
F 17 A
G T 0
H T 3 G
2
25
A
H
B
F
E
D
C
G
9
7
2
10
18
3
4
3
7
5
8
9
4
3
10
Select minimum distance
K d
v
p
v
A T 7 H
B T 12 H
C T 16 B
D T 2 G
E 22 B
F 17 A
G T 0
H T 3 G
2
25
A
H
B
F
E
D
C
G
9
7
2
10
18
3
4
3
7
5
8
9
4
3
10
Update unselected nodes
K d
v
p
v
A T 7 H
B T 12 H
C T 16 B
D T 2 G
E 22 B
F 17 A
G T 0
H T 3 G
2
25
A
H
B
F
E
D
C
G
9
7
2
10
18
3
4
3
7
5
8
9
4
3
10
Select minimum distance
K d
v
p
v
A T 7 H
B T 12 H
C T 16 B
D T 2 G
E 22 B
F T 17 A
G T 0
H T 3 G
2
25
A
H
B
F
E
D
C
G
9
7
2
10
18
2
4
3
7
5
8
9
4
3
10
Update unselected nodes
K d
v
p
v
A T 7 H
B T 12 H
C T 16 B
D T 2 G
E 19 F
F T 17 A
G T 0
H T 3 G
25
A
H
B
F
E
D
C
G
9
7
2
10
18
2
4
3
7
5
8
9
4
3
10
Select minimum distance
K d
v
p
v
A T 7 H
B T 12 H
C T 16 B
D T 2 G
E T 19 F
F T 17 A
G T 0
H T 3 G
Done
Minimum Spanning Tree
Let G = (V, E) be a simple, connected, undirected graph that is not
edge-weighted.

A spanning tree of G is a sub graph with all the vertices .

A MST for a for a weight graph is a spanning tree with minimum weight

Thus a minimum spanning tree for G is a graph, T = (V, E) with the
following properties:
V = V
T is connected
T is acyclic.

MST Property For a weighed connected Graph G, and T, a spanning
tree for G. Suppose that for every edge uv of G that is not in T, if uv is
added to T it creates a cycle such that uv is the max weighed edge on
the cycle. Then T is said to have the MST property
Constructing Spanning Trees
Any traversal of a connected, undirected graph
visits all the vertices in that graph. The set of
edges which are traversed during a traversal
forms a spanning tree.



For example, Fig:(b) shows the spanning tree
obtained from a breadth-first traversal starting at
vertex b.




Similarly, Fig:(c) shows the spanning tree
obtained from a depth-first traversal starting at
vertex c.

(a) Graph G
(b) Breadth-first
spanning tree of
G rooted at b
(c) Depth-first
spanning tree of
G rooted at c
MST - Example
For an edge-weighted , connected, undirected graph, G, the total
cost of G is the sum of the weights on all its edges.
A minimum-cost spanning tree for G is a minimum spanning tree of
G that has the least total cost.
Example: The graph
Has 16 spanning trees. Some are:
The graph has two minimum-cost spanning trees, each with a cost of 6:
Applications of Minimum-Cost Spanning Trees
Minimum-cost spanning trees have many applications. Some are:
Building cable networks that join n locations with minimum cost.
Building a road network that joins n cities with minimum cost.
Obtaining an independent set of circuit equations for an electrical
network.
In pattern recognition minimal spanning trees can be used to find noisy
pixels.

Kruskal's Algorithm.
Kruskals algorithm also finds the minimum cost
spanning tree of a graph by adding edges one-by-one.

enqueue edges of G in a queue in increasing order of cost.
T = | ;
while(queue is not empty){
dequeue an edge e;
if(e does not create a cycle with edges in T)
add e to T;
}
return T;
Example for Kruskals Algorithm.
Trace Kruskal's algorithm in finding a minimum-cost spanning tree for the
undirected, weighted graph given below:
The minimum cost is: 24

You might also like