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

Ch8 Graph

The document discusses different graph concepts like vertices, edges, paths, cycles, connected graphs, degree, complete graphs, weighted graphs, trees, graph representations using adjacency matrix and adjacency list. It also explains graph traversal algorithms breadth first search and depth first search including their implementations and applications.

Uploaded by

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

Ch8 Graph

The document discusses different graph concepts like vertices, edges, paths, cycles, connected graphs, degree, complete graphs, weighted graphs, trees, graph representations using adjacency matrix and adjacency list. It also explains graph traversal algorithms breadth first search and depth first search including their implementations and applications.

Uploaded by

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

Mr.

Abhijit Sarkar
Asst. Professor
Dept. of CSE (Cyber Security)
Haldia Institute of Technology
Graph:-
 A graph G consists of a set V of vertices(nodes) and a set E of edges(arcs).
 G=(V,E)
 V Finite, nonempty set of vertices.
 E Set of pairs of vertices, their pairs are called edges.
Adjacent Vertices:-
 Vertex v1 is said to be adjacent to a vertex v2 if there is an edge (v1,v2) or (v2,v1).

 Vertices adjacent to node 2 arc 1 and 3.


Path:-
 A path from vertex W is a sequence of vertices, each adjacent to the next.
 Ex:- 1,2,3 is a path.

Cycle:-
 A cycle is a path in which first and last vertices are the same.
 Ex:- (1,2,3,4,1) is a cycle.
Connected Graph:-
 A Graph is called connected if there is a path from any vertex to any other vertex.
 Path can be found in every pair of distinct vertices.

Unconnected Graph Connected Graph

Degree:-
 The number of edges incident on a vertex determine its degree.
 Ex:- Degree of 2 in the connected graph shown above is 3.
Complete Graph:-
 A graph is said to be complete or fully connected if there is a path from every vertex to every
other vertex.

Weighted Graph:-
 A graph is said to be weighted graph if every edge in the graph is assigned some weight or
value.
Tree:-
 A graph is a tree if its connected and if there are no cycles in the graph.
1) Adjacency Matrix:-
The adjacency matrix A for a graph G=(V,E) with n vertices, is an nxn matrix of bits such that,
Aij=1; if there is an edge from Vi to Vj.
Aij=0; if there is no such edge.
2) Adjacency List:-
 We store a graph as a linked structure.
 We store all the vertices in a list, then, for each vertex, we have a linked list of its adjacent
vertices.
3) Multilist Representation:-
 Lists in which nodes may be shared among several lists.
 An edge is shared by two different path.
 The following structure is used in multilist:
3) Multilist Representation:-

List:
Vertex 0:- N0N1N2
Vertex 1:- N0N3N4
Vertex 2:- N1N3N5
Vertex 3:- N2N4N5
1) Breadth First Traversal(BFS):-

 In BFS, one node is selected as the start position.


 It is visited and marked, then all unvisited nodes adjacent to it are visited and marked.
 Finally, unvisited nodes immediately adjacent to these nodes are visited and marked and so forth,
until entire graph has been traversed.
BFS(G,S)
{
for each vertex u∈V do
{
u.color=WHITE;
}
Q={S};
Q.vertex.color=GREY;
while(Q not empty) do
{
u=Remove(Q);
for each v∈(uAdj[])do
{
if(v.color==WHITE) then
{
v.color=GREY;
Add v to Q;
}
u.color=BLACK;
}
}
BFS(G,S)
{
for each vertex u∈V do
{
Legend:-
u.color=WHITE;
WHITE: Not visited/discovered
}
GREY: Discovered but not fully explored
Q={S}; BLACK: Discovered and fully explored
Q.vertex.color=GREY;
while(Q not empty) do
{
u=Remove(Q);
for each v∈(uAdj[])do
{
if(v.color==WHITE) then
{
v.color=GREY;
Add v to Q;
} Time Complexity:-
u.color=BLACK; O(V+E)
}
}
BFS(G,S)
{
for each vertex u∈V do
{
u.color=WHITE;
}
Q={S};
Q.vertex.color=GREY;
while(Q not empty) do
{
u=Remove(Q);
for each v∈(uAdj[])do
{
if(v.color==WHITE) then
{
v.color=GREY;
Add v to Q;
}
u.color=BLACK;
}
}
Shortest Path and Minimum Spanning Tree for unweighted graph: In an unweighted graph
with Breadth First, we always reach a vertex from given source using the minimum number
of edges.
Peer to Peer Networks. In Peer to Peer Networks like BitTorrent, Breadth First Search is used
to find all neighbor nodes.
Crawlers in Search Engines: Crawlers build index using Breadth First. The idea is to start
from source page and follow all links from source and keep doing same.
Social Networking Websites: In social networks, we can find people within a given distance
‘k’ from a person using Breadth First Search till ‘k’ levels.
GPS Navigation systems: Breadth First Search is used to find all neighboring locations.
Cycle detection in undirected graph: In undirected graphs, either Breadth First Search or
Depth First Search can be used to detect cycle. We can use BFS to detect cycle in a directed
graph also.
Ford–Fulkerson algorithm: In Ford-Fulkerson algorithm, we can either use Breadth First or
Depth First Traversal to find the maximum flow.
To test if a graph is Bipartite We can either use Breadth First or Depth First Traversal.
Path Finding: We can either use Breadth First or Depth First Traversal to find if there is a
path between two vertices.
1) Depth First Traversal(DFS):-
 BFS proceeds level-by-level, DFS follows first a path from the starting node to an ending node.
 Then, another path from the start to end and so forth until all nodes have been visited.
 It uses to concept of backtrack.
DFSVisit(G)
{
for each vertex u∈V do
{
u.color=WHITE;
}
for each vertex u∈V
{
if u.color==WHITE then,
DFS(u);
}
}
DFS(u)
{
u.color=GREY;
for each v∈(uAdj[])do
{
if v.color==WHITE then
DFS(v);
}
u.color=BLACK;
}
DFSVisit(G)
{
for each vertex u∈V do
{ Legend:-
u.color=WHITE;
} WHITE: Not visited/discovered
for each vertex u∈V GREY: Discovered but not fully explored
{ BLACK: Discovered and fully explored
if u.color==WHITE then,
DFS(u);
}
}
DFS(u)
{
u.color=GREY;
for each v∈(uAdj[])do
{
if v.color==WHITE then
DFS(v);
Time Complexity:-
}
O(V+E)
u.color=BLACK;
}
DFSVisit(G)
{
for each vertex u∈V do
{
u.color=WHITE;
}
for each vertex u∈V
{
if u.color==WHITE then,
DFS(u);
}
}
DFS(u)
{
u.color=GREY;
for each v∈(uAdj[])do
{
if v.color==WHITE then
DFS(v);
}
u.color=BLACK;
}
 Detecting cycle in a graph: A graph has cycle if and only if we see a back edge during DFS.
So we can run DFS for the graph and check for back edges.
 Path Finding: We can specialize the DFS algorithm to find a path between two given vertices
u and z.
i) Call DFS(G, u) with u as the start vertex.
ii) Use a stack S to keep track of the path between the start vertex and the current vertex.
iii) As soon as destination vertex z is encountered, return the path as the contents of the stack
 Topological Sorting: Topological Sorting is mainly used for scheduling jobs from the given
dependencies among jobs.
 To test if a graph is bipartite: We can augment either BFS or DFS when we first discover a
new vertex, color it opposited its parents, and for each other edge, check it doesn’t link two
vertices of the same color. The first vertex in any connected component can be red or
black!
 Finding Strongly Connected Components of a graph: A directed graph is called strongly
connected if there is a path from each vertex in the graph to every other vertex.
 Solving puzzles with only one solution, such as mazes.
BFS DFS
Full form BFS stands for Breadth First Search. DFS stands for Depth First Search.

Technique It a vertex-based technique to find the It is an edge-based technique because the vertices
shortest path in a graph. along the edge are explored first from the starting
to the end node.

Definition BFS is a traversal technique in which all DFS is also a traversal technique in which traversal
the nodes of the same level are is started from the root node and explore the nodes
explored first, and then we move to the as far as possible until we reach the node that has
next level. no unvisited adjacent nodes.
Data Queue data structure is used for the Stack data structure is used for the BFS traversal.
Structure BFS traversal.
Backtracking BFS does not use the backtracking DFS uses backtracking to traverse all the unvisited
concept. nodes.
Number of BFS finds the shortest path having a In DFS, a greater number of edges are required to
edges minimum number of edges to traverse traverse from the source vertex to the destination
from the source to the destination vertex.
vertex.
Optimality BFS traversal is optimal for those DFS traversal is optimal for those graphs in which
vertices which are to be searched closer solutions are away from the source vertex.
to the source vertex.
Speed BFS is slower than DFS. DFS is faster than BFS.
Suitability for It is not suitable for the decision tree It is suitable for the decision tree. Based on the
decision tree because it requires exploring all the decision, it explores all the paths. When the goal is
neighboring nodes first. found, it stops its traversal.

Memory It is not memory efficient as it requires It is memory efficient as it requires less memory
efficient more memory than DFS. than BFS.

You might also like