Digital Assignment Theory 18bca0045
Digital Assignment Theory 18bca0045
Digital Assignment Theory 18bca0045
1. In undirected graphs, either Breadth First Search or Depth First Search can
be used to detect cycle. How? Provide reasons and justify your answer with
an example.
We have discussed cycle detection for directed graph. We have also discussed a union-
find algorithm for cycle detection in undirected graphs.. The time complexity of the union-
find algorithm is O(ELogV). Like directed graphs, we can use DFS to detect cycle in an
undirected graph in O (V+E) time. We have discussed DFS based solution for cycle
detection in undirected graph.
In this article, BFS based solution is discussed. We do a BFS traversal of the given graph.
For every visited vertex ‘v’, if there is an adjacent ‘u’ such that u is already visited and u is
not parent of v, then there is a cycle in graph. If we don’t find such an adjacent for any
vertex, we say that there is no cycle. The assumption of this approach is that there are no
parallel edges between any two vertices.
An undirected graph is graph, a set of objects (called vertices or nodes) that are
connected together, where all edges are bidirectional.
Breadth First Traversal (or Search) for a graph is similar to Breadth First Traversal of a
tree (See method 2 of this post). The only catch here is, 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. For simplicity, it is assumed that all vertices are
reachable from the starting vertex.
For example, in the following graph, we start traversal from vertex 2. When we come to
vertex 0, we look for all adjacent vertices of it. 2 is also an adjacent vertex of 0. If we don’t
mark visited vertices, then 2 will be processed again and it will become a non-terminating
process. A Breadth First Traversal of the following graph is 2, 0, 3, 1.
Depth First Traversal (or Search) for a graph is similar to Depth First Traversal of a tree.
The only catch here is, unlike trees, graphs may contain cycles, so we may come to the
1
same node again. To avoid processing a node more than once, we use a boolean visited
array.
For example, in the following graph, we start traversal from vertex 2. When we come to
vertex 0, we look for all adjacent vertices of it. 2 is also an adjacent vertex of 0. If we don’t
mark visited vertices, then 2 will be processed again and it will become a non-terminating
process. A Depth First Traversal of the following graph is 2, 0, 1, 3.
Depth first search is more memory efficient than breadth first search as you can
backtrack sooner. It is also easier to implement if you use the call stack but this relies on
the longest path not overflowing the stack.
• DFS is easier to implement.
• Once DFS finds a cycle, the stack will contain the nodes forming the cycle.
The same is not true for BFS, so you need to do extra work if you want to also print the
found cycle. This makes DFS a lot more convenient.
2. In directed graph, only depth first search can be used. Why? Provide
reasons and justify your answer with an example.
7) Solving puzzles with only one solution, such as mazes. (DFS can be adapted to
find all solutions to a maze by only including nodes on the current path in the visited
set.)
use DFS (or any other search) to construct a list of vertices reachable from the starting
vertex. Do this silently, print nothing. Then find a vertex V in the list such that there is no
edge from a vertex in the list to V, print V, and remove V from the list. Repeat this step
until the list is empty.
1 Depth First Search in Directed Graphs
Let tt = ( V, E) be a directed graph, where V is the vertex set and E is the edge
set. We assume the graph is represented as an adjacency structure, that is,
for every vertex v there is a set adj(v) which is the set of vertices reachable
by following one edge out of v. To do a depth first search we keep two pieces
of information associated with each vertex v. One is the depth first search
numbering num(v), and the other is mark(v), which indicates that v is
currently on the recursion stack.
Here is the depth first search procedure:
i←0
for all v ∈ V do num(v) ← 0
for all v ∈ V do mark(v) ←
0 for all v ∈ V do
if num(v) = 0 then DFS(v)
DFS(v)
i←i+1
num(v) ← i
mark(v) ←
1
for all w ∈ adj(v) do
if num(w) = 0 then DFS(w) [(v, w) is a tree edge]
3
else if num(w) > num(v) then [(v, w) is a forward
edge] else if mark(w) = 0 then [(v, w) is a cross
edge]
else [(v, w) is a back edge]
mark(v) ←
0 end DFS
This process examines all the edges and vertices. The call DFS( v) is made
exactly once for each vertex in the graph. Each edge is placed into exactly
one of four classes by the algorithm: tree edges, forward edges, cross
edges and back edges.
4
This classification of the edges is not a property of the graph alone. It also
depends on the ordering of the vertices in adj(v) and on the ordering of the
vertices in the loop that calls the DFS procedure. The num and mark fields
are not actually necessary to accomplish a complete search of the graph.
All that is needed to do that is a single bit for each vertex that indicates
whether or not that vertex has already been searched. (This bit is zero for
vertex v if and only if num(v) = 0.) We have presented the fully general
version here because it is needed for the strong components algorithm
that we present later in this lecture.
The tree edges have the property that either zero or one of them points to
a given vertex. (It’s the edge used to discover a vertex for the first time.)
Therefore, they define a collection of trees, called the depth first spanning
forest of the graph. The root of each tree is the lowest numbered vertex in
it (the one that was searched first). These rooted trees allow us to define
the ancestor and descendant relations among vertices.
3. In GPS Navigation System, Breadth First Search is being used. Why? Why not
Depth First Search. Find Reasons and justify your answer.
GPS in BFS:
There are differences in the route which I usually take and the one which GPS shows as
the shortest, probably due to the algorithms used. I learned from my graph theory data
structure classes that (BFS) Breadth First search example is GPS navigation and digital
maps. I tried looking for the possible use of Algorithms (Breadth First Search example
or A* application) used in GPS navigation on the web, but I couldn’t find a lot of details.
So here is how Breadth First Search is used in real life application like GPS.
Digital maps, unlike humans, see streets as a bunch of nodes. The 2.6-mile road from
the Columbus Circle station (59 st) to Cathedral Pkwy (110 st) is called Central Park
West. We (humans) consider this road a single entity (You may divide it into few more
segments based on metro stations or intersections, but not more than that).
5
There are differences in the route which I usually take and the one which GPS
shows as the shortest, probably due to the algorithms used. I learned from my
graph theory data structure classes that (BFS) Breadth First search example is GPS
navigation and digital maps. I tried looking for the possible use of Algorithms
(Breadth First Search example or A* application) used in GPS navigation on the
web, but I couldn’t find a lot of details. So here is how Breadth First Search is used
in real life application like GPS.
Let’s first understand working of GPS navigation
Digital maps, unlike humans, see streets as a bunch of nodes. The 2.6-mile road
from the Columbus Circle station (59 st) to Cathedral Pkwy (110 st) is called Central
Park West. We (humans) consider this road a single entity (You may divide it into
few more segments based on metro stations or intersections, but not more than
that).
But a GPS navigation or any other digital map divides it into hundreds of segments,
with some only 24 meters long. A GPS looks at this street as a graph divided into
vertices and edges.
Considering this, there is a lot of data to be covered and calculated while finding
the shortest path.
Before we begin you must know the answers to the following:
graph
A graph usually looks like the image below and is made up of vertices and edges
(represented by lines and circles, respectively).
6
4.
Consider any graph with minimum of 8 nodes and all the nodes are connected
without any isolated node. Find all the possible paths between any two nodes
among them. Implement a suitable program to print the same.
• 2 ->0-> 3-> 4
• 2 ->1-> 3-> 4
• 2 ->5-> 6-> 4
PROGRAM:
#include<iostream>
#include<list>
class Graph
};
7
Graph::Graph(int V)
this->V = V;
8
visited[i] = false;
visited[u] = true;
path[path_index] = u;
path_index++;
if (u == d)
list<int>::iterator i;
if (!visited[*i])
9
}
path_index--;
visited[u] = false;
int main()
Graph g(8);
g.addEdge(0, 1);
g.addEdge(0, 2);
g.addEdge(0, 3);
g.addEdge(2, 0);
g.addEdge(2, 1);
g.addEdge(1, 3);
g.addEdge(2,5);
g.addEdge(5,2);
g.addEdge(5,6);
g.addEdge(6,5);
1
0
g.addEdge(6,4);
g.addEdge(4,6);
g.addEdge(4,7);
g.addEdge(7,4);
g.addEdge(4,3);
g.addEdge(3,4);
int s,d;
cout<<"Enter source:";
cin>>s;
cout<<"Enter destination:";
cin>>d;
cout << "Following are all different paths from "<<s << "to"<<d<<endl;
g.printAllPaths(s,d);
return 0;
1
1
5.
Discuss the applications of Binary Tree and Binary Search Tree with a
suitable real time example.
1
2
perform much worse than self-balancing binary trees for searching, there are
many binary trees (such as binary tries) for which "balancing" has no meaning.
• The reason that binary trees are used more often than n-ary trees for
searching is that n-ary trees are more complex, but usually provide no real
speed advantage.
In a (balanced) binary tree with m nodes, moving from one level to the next requires one
comparison, and there are log_2(m) levels, for a total of log_2(m) comparisons. In
contrast, an n-ary tree will it will require log_2(n) comparisons (using a binary search)
to move to the next level. Since there are log_n(m) total levels, the search will require
log_2(n)*log_n(m) = log_2(m) comparisons total. So, though n-ary trees are more
complex, they provide no advantage in terms of total comparison.
Real world example (on daily basis) of binary trees which I could think of is
1) family tree.
1
3
Binary Search Tree, is a node-based binary tree data structure which has the
following properties:
• The left subtree of a node contains only nodes with keys lesser than the node’s
key.
• The right subtree of a node contains only nodes with keys greater than the node’s
key.
• The left and right subtree each must also be a binary search
tree. There must be no duplicate nodes.
1
4
1
5
1
6