Ch8 Graph
Ch8 Graph
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).
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.
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:- N0N1N2
Vertex 1:- N0N3N4
Vertex 2:- N1N3N5
Vertex 3:- N2N4N5
1) Breadth First Traversal(BFS):-
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.