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

Data Structures Lab 12 Graphs BFS DFS - R

Uploaded by

doravivek6
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views

Data Structures Lab 12 Graphs BFS DFS - R

Uploaded by

doravivek6
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 50

Data Structures Lab – BFS & DFS

Outline
• Introduction
• Graph Terminology
• Representation of Graphs
• BFS
• DFS
• Exercise
• Prelab Questions
GRAPHS
 In real world many problems are represented in terms of objects and connections between them.

 Leonhard Euler used graph theory to solve the classical Konigsberg bridge problem.

 Example: In an airline route map, questions could be like :


 What is the fastest way to go from Hyderabad to SFO
 What is the cheapest way to go from Hyderabad to SFO

 Graphs are data structures used for solving these kind of problems.

 Graph : is a ordered pair (V, E)


Where V is a finite, non-empty set of nodes, called Vertices
E is a set finite, possibly empty, of pairs of vertices, called Edges
G=(V,E) represents a Graph.
V(G) and E(G) represent the sets of vertices and edges of G, respectively.
Example:

Edge : (1,4 )can also be considered as (4,1) if the edge is


undirected.
Terminology
 Edge : has two vertices (u,v) to which it is attached, the vertices are called endpoints
 When an edge connects two vertices, the vertices are said to be adjacent to each
other and the edge is incident on both vertices.
 Two vertices are called adjacent if they are endpoints of the same edge.
 Directed Edge: ordered pair of vertices (u,v), where vertex-u is the origin(tail) and
vertex-v is destination(head)
 Example : One-way road traffic, here (u,v) is different from (v, u) u v
 Undirected Edge: unorderred pair of vertices (u, v)
 Example: railway line, here (u,v) is also (v,u) u v
 Directed Graph : All edges are directed. Example : Route network

 Undirected Graph: All edges are undirected. Example: Flight network


Terminology
 A self loop is an edge that connects a vertex to itself
 Two edges are parallel if they connect the same pair of vertices
 The degree of a vertex is the number of edges that are incident on it.
 External vertex : degree = 1
 Internal vertex : degree = 2 or more
 Isolated vertex : degree = 0
 Outgoing edges of a vertex are directed edges that the vertex is the origin of.
 Incoming edges of a vertex are directed edges that the vertex is destination of.
 In a directed graph, outdegree of a vertex is the number of outgoing edges from it, and
indegree is the number of incoming edges.
 Subgraph : subset of a graph’s edges ( with associated vertices) that form a graph.
Terminology
 Subgraph : subset of a graph’s edges ( with associated vertices) that form a graph.

 A graph may not have multiple occurrences of the same edge. If we remove this
restriction we obtain a data object referred to as a Multigraph.
Terminology
 Path: a sequence of adjacent vertices.
 Simple Path : path with no repeated vertices

The dotted lines represent a simple path from G to E

 Cyclic Path: is a path where the first and the last vertices are same.

The dotted lines represent a cyclic path from C to C


 Length of a path is the number of edges on it.
 We say a vertex is connected to another vertex if there is a path that contains both of
them.
Terminology
 Connected Graph: A graph is connected if there is a path from every vertex to every
other vertex.

A connected graph has path between every pair of vertices. There is no unreachable vertex.
 A disconnected graph is a one which is not connected, it consists of a set of connected
components
[forest – disjoint union of tree]

 A graph with no cycles is called a Tree. A tree is an acyclic, connected graph

 maximum number of edges in an acyclic undirected graph with n vertices : n-1


Terminology
 Complete Graph: A graph with all edges present is called a complete graph. i.e. Every
two vertices are adjacent
 for undirected graph with n vertices, the maximum number of edges is n(n-1)/2

 for directed graph with n vertices, the maximum number of edges is n(n-1)

Sparse Graph : A graph with relatively fewer edges. E < |V| log |v|
 A Directed Acyclic Graph (DAG) : is a directed graph with no cycles.

 Weighted graphs: integers (weights) are assigned to each edge to represent (distances
or costs).

 Note: A directed, weighted graph is called a network


Terminology
 Simple Graph: as opposed to a multigraph, it is an undirected graph in which both
multiple edges and loops are disallowed. In a simple graph with n vertices, the degree
of every vertex is at most n-1
GRAPH ADT
objects: a nonempty set of vertices and a set of undirected edges, where each edge is a pair
of
Vertices

functions:
Graph Create() = return an empty graph
Graph InsertVertex(graph, v) = return a graph with v inserted. v has no incident edge.
Graph InsertEdge(graph, v1,v2) = return a graph with new edge between v1 and v2
Graph DeleteVertex(graph, v) = return a graph in which v and all edges incident to it are
removed
Graph DeleteEdge(graph, v1, v2) = return a graph in which the edge (v1, v2) is removed
Boolean IsEmpty(graph) = if (graph==empty graph) return TRUE else return FALSE
List Adjacent(graph,v) = return a list of all vertices that are adjacent to v

Applications of Graphs:
 Representing relationships between components in electronic circuits
 Transportation networks: Highway network, Flight network
 Computer networks: Local area network, Internet, Web
 Databases: For representing ER (Entity Relationship) diagrams in databases, for
 representing dependency of tables in databases
GRAPH Representation
As in other ADTs, to manipulate graphs we need to represent them in some useful form.
Basically, there are different ways of doing this:
• Adjacency Matrix
• Adjacency List

Adjacency Matrix
In this method, we use a matrix with size V × V ( V is number of vertices)
The values of matrix are boolean.
Let us assume the matrix is Adj.
The value Adj[u, v] is set to 1 if there is an edge from vertex u to vertex v and 0 otherwise.
GRAPH Representation
Adjacency Matrix
GRAPH Representation
Adjacency Matrix
GRAPH Representation
Adjacency List
GRAPH Representation
Adjacency List
GRAPH Representation
Adjacency List
Comparing Adjacency matrix and List
 From the adjacency matrix, to determine the connection of vertices is easy
 Deleting a node is easier in adjacency matrix than from a list.

In adjacency list representation:


 space is saved for sparse graphs(graph with very few edges when compared to number of nodes).
 DFS and BSF can be done in O(V + E) time for adjacency list representation.
These operations take O(V^2) time in adjacency matrix representation.
Here is V and E are number of vertices and edges respectively.
 Adding a vertex in adjacency list representation is easier than adjacency matrix
representation.

Elementary Graph Operations


 To solve problems on graphs, we need a mechanism for traversing a graph.
 Graph traversal algorithms are called Graph Search algorithms.
 Search algorithms start at some source vertex in a graph and “search” the graph by going through
the edges and marking the vertices.
 Depth First Search (DFS) traversal
 Breadth First Search (BFS) traversal
Depth First Search
 DFS algorithm works in a manner similar to preorder traversal of the trees.
 Like preorder traversal, internally this algorithm also uses stack.
 Unlike trees, graphs may contain cycles, so we may come to the same node again. To
avoid processing a node more than once, we use a boolean visited array.
 Traversal can start from any vertex Vi. Vi is marked as visited, next we select an unvisited
vertex Wi from V’i s adjacency list and carry out DFS on Wi
 Eventually our search reaches a vertex u, that has no unvisited vertices on its adjacency
list. At this point we remove a vertex from the stack and continue processing its
adjacency list.
 Previously visited vertices are discarded; unvisited vertices are visited and placed on the
stack.
 The search ends when the stack is empty.
ALGORITHM:
void dfs(int v)
{
node_pointer w;
visited[v]= TRUE;
printf(“%4d”, v);
for (w=graph[v]; w; w=w->link)
if (!visited[w->vertex])
dfs(w->vertex);
}
DFS ALGORITHMs

 DFS algorithm
n  number of vertices
//Initialize visited[] to false(0)
for(i=0; i<n; i++)
visited[i]=0;
void DFS(vertex i)
{
visited[i] = 1;
for each w adjacent to I
if(!visited[w])
DFS(w);
}
DFS Example 1

 depth first search starting at V0: v0, v1, v3, v7, v4, v5, v2, v6
DFS Example 2
 illustration for an Undirected Graph :
DFS Example 2
DFS Example 2
DFS Example 3
DFS Example 3
DFS Example 3
Applications of DFS
• The time complexity of DFS is O(V + E), if we use adjacency lists for representing the
graphs.
• This is because we are starting at a vertex and processing the adjacent nodes only if
they are not visited.
• Similarly, if an adjacency matrix is used for a graph representation, then all edges
adjacent to a vertex can’t be found efficiently, and this gives O(V2) complexity.

 Topological sorting
 Finding connected components
 Finding articulation points (cut vertices) of the graph
 Finding strongly connected components
 Solving puzzles such as mazes
Breadth First Search
 BFS algorithm works similar to level-order traversal of Trees.
 BFS also uses a Queue, like Level order traversal
 BFS starts at vertex V and marks it as visited
 It then visits each of the vertices on V’s adjacency list. When all the vertices on V’s adjacency list have
been visited, it visits all the unvisited vertices that are adjacent to the first vertex on V’s adjacency list.
 To implement this scheme, as we visit each vertex we place the vertex in a queue. When we have
exhausted an adjacency list, we remove a vertex from the queue and proceed by examining each of the
vertices on its adjacency list.
 Unvisited vertices are visited and then placed on the queue, visited vertices are ignored.
 The search is complete when the queue is empty
ALGORITHM:

void BFS(int v) {
node_ptr w;
front=rear=NULL; // initialize queue
printf(“%4d”,v); visited[v]=TRUE; addq(v);
while(front) {
v = deleteq( );
for(w=graph[v]; w; w=w->link)
{ if(!visited[w->vertex])
{
printf(“%4d”, w->vertex);
addq(w->vertex);
visited[w->vertex] = TRUE;
}
}
}
}
BFS ALGORITHM

Algorithm BFS( G, S)
initialize queue Q
visited(S)=TRUE; Enqueue (Q,S);
while(!Q.empty) do
v=Dequeue(Q); print(v);
for all vertices w adjacent to v do
if(visisted(w)==FALSE) then
{
Enqueue(Q,w);
visited(w)=TRUE;
}
end for
end while
BFS Example 1

 Breadth first search starting at V0: v0, v1, v2, v3, v4, v5, v6, v7
BFS Example 2
 illustration for an Undirected Graph :
BFS Example 2
BFS Example 2
BFS Example 3
BFS Example 3
Applications of BFS
• Time complexity of BFS is O(V + E), if we use adjacency lists for representing the graphs,
and O(V2) for adjacency matrix representation.

 Finding all connected components in a graph


 Finding all nodes within one connected component
 Finding the shortest path between two nodes
 Testing a graph for bipartiteness
Comparing BFS and DFS

 Comparing BFS and DFS, the big advantage of DFS is that it has much lower memory
requirements than BFS because it’s not required to store all of the child pointers at each
level.
 Depending on the data and what we are looking for, either DFS or BFS can be
advantageous.
 For example, in a family tree if we are looking for someone from younger generation and
if we assume that person would be at the bottom of the tree, then DFS is a better choice.
BFS would take a very long time to reach that last level. The DFS algorithm finds the goal
faster.
 Now, if we were looking for a family member who belongs to a generation from very long
time ago, then that person would be closer to the top of the tree. In this case, BFS finds
faster than DFS.
 So, the advantages of either vary depending on the data and what we are looking for.
 DFS is related to preorder traversal of a tree. Like preorder traversal, DFS visits each node
before its children.
 The BFS algorithm works similar to level – order traversal of the trees.
 BFS visits each level one at a time, and if we know the solution we are searching for is at
a low depth, then BFS is good. DFS is a better choice if the solution is at maximum depth.
 The below table in the next slide the differences between DFS and BFS in terms of their
applications.
Comparing BFS and DFS
Minimum Cost Spanning Tree
 The spanning tree of a graph is subgraph that contains all the vertices and is also a tree [no cycles]
 A graph may have many spanning trees.

 The cost of a spanning tree of a weighted undirected graph is the sum of the costs(weights) of the
edges in the spanning tree.
 A minimum cost spanning tree is a spanning tree of least cost
 A spanning tree is a minimal subgraph G’ of G such that V(G’) is same as V(G) and G’ is connected
– A minimal subgraph is one with the fewest number of edges
 The solution must satisfy the following constraints:
 We must use only edges within the graph
 We must use exactly n-1 edges (where n is number of vertices)
 We may not use edges that would produce a cycle.
Kruskal’s Algorithm
 Edge by edge
 O(e log e)
 At each stage Kruskal’s forms a forest
 ALGORITHM:
T={};
while ((T has less than n-1 edges) &&(E not empty)) do
{
choose an edge (v,w) from E of lowest cost;
delete (v,w) from E;
if((v,w) does not create a cycle in T)
add(v,w) to T;
else
discard(v,w);
}
if(T contains fewer than n – 1 edges)
printf(“No spanning tree\n”);
Kruskal’s Example
Kruskal’s Example
Prim’s Algorithm
 Begins from a vertex
 O(n2)
 At each stage Prim’s forms a Tree
 ALGORITHM:

T={};
TV = {1}; /* start with vertex 1 and no edges */
While (T contains fewer than n-1 edges)
{
let(u,v) be a least cost edge such that u ∈ TV and v ∉ TV;
if (there is no such edge)
break;
add v to TV;
add (u,v) to T;
}
if (T contains fewer than n-1 edges)
printf(“No spanning tree\n”);
Prim’s Example
Prim’s Example 2
Applications of Graphs

 Representing relationships between components in electronic circuits


 Transportation networks: Highway network, Flight network
 Computer networks: Local area network, Internet, Web
 Databases: For representing ER (Entity Relationship) diagrams in databases, for representing
dependency of tables in databases
 Google maps uses graphs for building transportation systems, where intersection of two(or more)
roads are considered to be a vertex and the road connecting two vertices is considered to be an
edge, thus their navigation system is based on the algorithm to calculate the shortest path
between two vertices.
 In Facebook, users are considered to be the vertices and if they are friends then there is an edge
running between them. Facebook’s Friend suggestion algorithm uses graph theory.
 In Operating System, we come across the Resource Allocation Graph where each process and
resources are considered to be vertices. Edges are drawn from resources to the allocated process,
or from requesting process to the requested resource. If this leads to any formation of a cycle
then a deadlock will occur.
Exercise
• Implementation of Breadth First search Traversal on Graphs.
• Implementation of Depth First search Traversal on Graphs.
Prelab Questions
• Give the difference between DFS & BFS.
• Define spanning tree.
• Does DFS and BFS give the same output?
• Differentiate between Prim’s algorithm and
Kruskal’s algorithm.
• List the applications of Graphs.

You might also like