Lecture 05
Lecture 05
Lecture 05
Graph Algorithms-1
Topics
Graph terminology
Graph Representations
-Matrix Representation
-Linked list Representation
The number of vertices and edges are given by the cardinalities |V| and |E| of the
corresponding sets. The sample graph consists of four vertices and seven edges. Thus,
|V|= 4 and |E|=7
Graphs are usually represented by pictorial diagrams. The vertices can be shown as
labeled circles or rectangles. The edges are depicted as arcs or lines. Except for some
applications, in which distances among vertices are important , the positions of the
vertices are, in general, immaterial. Thus, graphs can be shown pictorially in several
ways.
Graphs
Undirected Graph
A graph G=(V,E) with vertex set V={v1, v2, v3,……vn } is called undirected if
(vi, vj) = ( vj, vi) for i ≠ j
An undirected graph is sometimes referred to as undigraph.
In pictorial representation of undirected graph, the edges are not assigned any direction.
An undirected complete graph , with n vertices, has n(n-1)/2 edges. The space complexity
of complete graph is O(n2) . A complete graph is dense. By contrast, a graph with space
complexity O(n lg n) is called sparse.
Graphs
Directed Graph
A graph G=(V,E) with vertex set V={v1, v2 ,v3,……vn } is called directed if
(vi, vj) ≠ ( vj, vi) for i ≠ j
In other words, the edges (vi, vj) and (vj, vi), associated with any pair of vertices vi, vj , are
considered distinct. In pictorial representation these are shown with arrows.
Example: The figure below shows an examples of the directed weighted graph. Note that
weight of edged from vertex a to vertex b is 73 and that from vertex b to vertex a is 35
Example: The figure below depicts a path P = [a, b, g, d, e, i, h ] in a sample graph. The
path is shown in bold red lines.
Path P=[a, b, g, d, e, i, h ]
The number of edges connecting the vertices in a path is called path length. The path length
in the above example is 6.
Graphs Paths
Simple Path
A path is called simple if no vertices are repeated; otherwise, the path is referred to as non-
simple.
Example: The figures below show simple and non-simple paths in a graph. The path
P1=[ a, c, g, d, f ] is simple. The path P2=[a, c, g, d, f, c, b, f ] is non-simple, because it
passes through vertices c, f twice
(a) A simple path P1=[a ,c ,g, d, f] (b) A non- simple path P2=[a ,c ,g, d, f, c, b, f]
Graphs Paths
Loops
A loop is special path that originates and terminates at a single node, and does not pass
through other vertices.
loop
loop
The loops are important in certain some applications. For example, loops represent
certain states in Finite State Automata
Graphs Paths
Cyclic and Acyclic Graphs
A path that originates and terminates at the same vertex, and links two or more vertices,
is called cycle. If a graph contains a cycle it is called cyclic. By contrast, a graph which
contains no cycles is known as acyclic.
Example: In diagram (i), the path P=[b, c, d, a] is a cycle. The diagram (ii) represents a
acyclic graph.
The adjacency matrix can be defined mathematically. Let G=(V,E) be a graph, with
V={v1,v2,……vn}, and E={(v1,v2),(v1,v3)….}
where n=|V|. The adjacency matrix A=[aij ] as
1 if (vi, vj) E
aij =
0 otherwise
The definition implies that entry in the ith row and jth column of the matrix is 1
if a link exists between the ith and jth vertex; otherwise, it is 0.
The size adjacency matrix is with vertex set V is |V|x|V| . Thus space complexity is θ(|V|2)
Graphs Representation
Adjacency Matrix
Example : Figure (i) shows a sample directed graph. Figure (ii) shows the adjacency
matrix for the graph
a b c d e f g
a 0 1 0 0 0 1 0
b 0 0 1 1 0 0 0
c 0 0 0 1 1 0 1
d 0 0 0 0 1 0 0
e 0 0 0 0 0 0 1
f 0 0 0 0 1 0 1
g 1 1 0 0 0 0 0
Let G=(V,E) be a graph, and Adj(u) be the linked list corresponding to vertex u, then
for all v,
v Adj(u), if (u ,v ) E.
The vertices belonging to Adj(u) are called neighbors of vertex u, or adjacent vertices.
Graphs Representation
Linked List
Example: The diagrams below show a sample graph and its linked list representation
a b h
b f i j
c b j
d c e
e d f j
f e i
g f h
h a i
j c d i
(ii) Linked list Representation
Graph Traversal
Procedures
In graph traversal, all vertices are visited once. Depending on the application, the data and
keys are accessed and processed further. For example, the traversal procedure can be used to
list keys stored in the vertices The two important procedures for graph traversal are:
The DFS and BFS algorithms can be used to traverse both directed and undirected graphs
Depth First Search
Depth First Search
Strategy
The Depth First Search (DFS) systematically visits all vertices of a graph. Initially, a start
vertex is chosen, and its neighbors are explored. Then neighbors and of neighbors are
explored. This process is continued till the remotest neighbor of the start vertex is reached,
which is called the dead end. The algorithm then backtracks to the start vertex and on the
way back lists all of the vertices in depth first search order. The depth first search algorithm
belongs to the class of backtracking algorithms.
A stack is used to implement the DFS procedure. The stack keeps track of vertices
traversed . For this purpose , the three states of vertices are designated as ready, wait and
processed, defined as follows :
Step #3: Repeat steps # 4 through Step #5 until the stack is empty
Step #4: Pop off an element from the stack. Process and mark it as visited (processed)
Step #5: Push to stack adjacent vertices of the processed vertex. Go to Step # 3.
The same procedure is applied to a subgraph, which may contain any unvisited vertices.
Depth First Search
Spanning Tree
Apart from extracting information stored in vertices, the DFS can be used to construct
special tree called DFS Spanning trees The Spanning trees are useful in investigating
important properties of the graphs and subgraphs
During the DFS, a spanning tree is constructed by linking together a vertex that is visited
for the first time to the vertex from which it is reached. The links are called tree edges
Depth First Search
Example
Consider the sample directed graph shown below. The steps involved in Depth First Search are
depicted in the next set of diagrams As the algorithm proceeds, the depth first spanning tree is
also generated
push/pos data
Example: Figure (i) shows a sample graph. Figure (ii) provides a DFS tree. The vertices
M.P ,R,Q,D remain unvisited and constitute a disconnected subgraph
Example: Figure (i) shows a sample directed graph. Figure (ii) provides a DFS tree. The
vertices L,G,A remain unvisited and constitute subgraph whose vertices are not accessible
from initial vertex M
Example: Figure (i) shows a sample graph . Figure (ii) contains a DFS Spanning Tree, which
has no back edges. The graph is therefore acyclic.
(i) Sample undirected graph. (ii) Undirected graph with DFS Tree.
There are no back edges.
DFS Applications
Cyclic Graph
An undirected graph is cyclic if the DFS spanning has back edges
Example: Figure (i) shows a sample graph . Figure (ii) contains the DFS spanning tree, which ha
back edges shown in blue color. The graph is therefore cyclic.
(i) Sample undirected graph. (ii) Undirected graph with DFS Tree.
with back edges, shown in blue color
Depth First Search
Implementation
The DFS implements depth-first-search procedure by using a stack S. The address s of first selected
vertex is passed to the method. The implementation uses an array state[] which stores status of vertices
as READY, WAIT, PROCESSED. The graph is represented by adjacency list Adj . The notation
Adj[u] denotes a neighbor of vertex u
DFS Visualization
Analysis of DFS
Running Time
The running time for DFS consists of following major components
Ti = Initializing vertices T
Ts = Scanning Adjacency list
The Graph G(V,E) has |V| vertices and |E| edges. Each vertex is initialized once.
Therefore,
Ti = |V|
The scanning of Adjacency list takes place for each vertex. The total time is
proportional to the number of edges scanned. Since |E| is the number of edges in G, in
worst case the algorithm explores all edges , therefore,
Ts = |E|
If a graph has any subgraph (which is not reachable from the initial vertex),
the procedure may be re-started by choosing a vertex from the unvisited subgraph.
Step #4: Remove an element from the queue. Process and mark it as visited (processed state)
Step #5: Enqueue all adjacent vertices of the processed vertex. Go to Step # 3.
The same algorithm may be applied to a subgraph, which contains any unvisited vertices.
Breadth First Search
Example
Consider the sample directed graph shown below. The steps involved in performing breadth
first search on the graph are depicted in the next set of diagrams. The execution of
algorithm also generates BFS Spanning Tree, which is shown in bold red lines.
(1) Initially, all the vertices are in (2) The vertex a is chosen as the
ready state. For the execution of BFS start vertex . The neighbors b, n, k
algorithm, an auxiliary data structure of a are pushed to the queue. The
queue is used, which holds the current status of all of the vertices is
vertices in wait state given by the legend placed at
bottom.
The vertex traversed so for is a
Breadth First Search
Example
(3) The vertex b is removed from the queue. (4) The vertex k is removed from the queue.
The vertex c, neighbor of b, is pushed to the The neighbor j of vertex k is pushed to the
queue queue
The vertices traversed so for are: The vertices traversed so for are:
a,b a,b,k
Breadth First Search
Example
(5) The vertex n is removed from the (6) The vertex c is removed from the queue.
queue. The neighbor l of vertex n is The neighbor d of vertex c is pushed to the
pushed to the queue queue
The vertices traversed so for are: The vertices traversed so for are:
a,b,k,n a,b,k,n,c
Breadth First Search
Example
(7) The vertex j is removed from the (8) The vertex l is removed from the queue.
queue. The neighbor i of vertex j is The neighbors e, m of vertex l are pushed to
pushed to the queue the queue
The vertices traversed so for are: The vertices traversed so for are:
a,b,k,n,c,j a,b,k,n,c,j,l
Breadth First Search
Example
(9) The vertex d is removed from the queue. (10) The vertex i is removed from the
The vertex d has no unvisited neighbor queue. The vertex i has no unvisited
The vertices traversed so for are: neighbor
a,b,k,n,c,j,l,d The vertices traversed so for are:
a,b,k,n,c,j,l,d,i
Breadth First Search
Example
(11) The vertex e is removed from the (12) The vertex m is removed from the
queue. The neighbor f of vertex e is queue. The neighbor g of vertex m is
pushed to the queue pushed to the queue
The vertices traversed so for are: The vertices traversed so for are:
a,b,k,n,c,j,l,d,I,e a,b,k,n,c,j,l,d,I,e,
Breadth First Search
Example
(13) The vertex f is removed from the
(14) The vertex g is removed from the
queue. The vertex f has no unvisited
queue. The vertex h neighbor of g is
neighbor
pushed to the queue
The vertices traversed so for are:
The vertices traversed so for are:
a,b,k,n,c,j,l,d,I,e,f
a,b,k,n,c,j,l,d,I,e,f,g
Breadth First Search
Example
(15) The vertex h is removed from the queue. The vertex h has no unvisited neighbors. Further,
the queue is empty. The breadth first search algorithm terminates
Breadth First Search
Implementation
The BFS implements breadth-first-search procedure by using a queue Q. The address s of first
selected vertex is passed to the method. The implementation uses an array state[] to keep
track of the state of vertices: READY, WAIT, PROCESSED. The graph is represented by adjace
BFS Visualization
Analysis of BFS
Running Time
The running time for BFS consists of following major components
Ti = Initializing vertices T
Ts = Scanning Adjacency list
The Graph G(V,E) has |V| vertices and |E| edges. Each vertex is initialized once. Therefore,
Ti = |V|
The scanning of the Adjacency list takes place for each of the dequeued vertices. In BSF all
links are are examined. The total time is proportional to the number of edges scanned. Since |E|
is the number of edges in G,
Ts = |E|