Graph
Graph
1. Null Graph
A graph is known as a null graph if there are no edges in the graph.
2. Trivial Graph
Graph having only a single vertex, it is also the smallest graph possible.
3. Undirected Graph
A graph in which edges do not have any direction. That is the nodes are
unordered pairs in the definition of every edge.
4. Directed Graph
A graph in which edge has direction. That is the nodes are ordered pairs in the
definition of every edge.
5. Connected Graph
The graph in which from one node we can visit any other node in the graph is
known as a connected graph.
6. Disconnected Graph
The graph in which at least one node is not reachable from a node is known as
a disconnected graph.
7. Regular Graph
The graph in which the degree of every vertex is equal to K is called K regular
graph.
8. Complete Graph
The graph in which from each node there is an edge to each other node.
9. Cycle Graph
The graph in which the graph is a cycle in itself, the minimum value of degree
of each vertex is 2.
There are multiple ways to store a graph: The following are the most common
representations.
• Adjacency Matrix
• Adjacency List
In this method, the graph is stored in the form of the 2D matrix where rows and
columns denote vertices. Each entry in the matrix represents the weight of the
edge between those vertices.
Example : To implement a Graph we will use an Adjacency Matrix, like the one
below.
To store data for each vertex, in this case the letters A, B, C, and D, the data is
put in a separate array that matches the indexes in the adjacency matrix, like this:
Tree is a restricted type of Graph Data Structure, just with some more rules.
Every tree will always be a graph but not all graphs will be trees. Linked
List, Trees, and Heaps all are special cases of graphs.
Advantages of Graph Data Structure:
A Graph is a non-linear data structure consisting of nodes and edges. The nodes
are sometimes also referred to as vertices and the edges are lines or arcs that
connect any two nodes in the graph.
1. Vertices (nodes): The points where edges meet in a graph are known
as vertices or nodes. A vertex can represent a physical object, concept,
or abstract entity.
2. Edges: The connections between vertices are known as edges. They
can be undirected (bidirectional) or directed (unidirectional).
3. Weight: A weight can be assigned to an edge, representing the cost or
distance between two vertices. A weighted graph is a graph where the
edges have weights.
4. Degree: The degree of a vertex is the number of edges that connect to
it. In a directed graph, the in-degree of a vertex is the number of edges
that point to it, and the out-degree is the number of edges that start
from it.
5. Path: A path is a sequence of vertices that are connected by edges. A
simple path does not contain any repeated vertices or edges.
6. Cycle: A cycle is a path that starts and ends at the same vertex. A
simple cycle does not contain any repeated vertices or edges.
7. Connectedness: A graph is said to be connected if there is a path
between any two vertices. A disconnected graph is a graph that is not
connected.
8. Planarity: A graph is said to be planar if it can be drawn on a plane
without any edges crossing each other.
9. Bipartiteness: A graph is said to be bipartite if its vertices can be
divided into two disjoint sets such that no two vertices in the same set
are connected by an edge.
What is Graph?
What is Tree?
A collection of nodes
A hierarchical data structure
(vertices) and edges,
consisting of nodes connected by
where edges connect
edges with a single root node.
Definition nodes.
Traversal can be
complex due to cycles Traversal is straightforward and
Traversal and disconnected can be done in linear time.
Complexity components.
To traverse a Graph means to start in one vertex, and go along the edges to visit
other vertices until all vertices, or as many as possible, have been visited.
Breadth First Search visits all adjacent vertices of a vertex before visiting
neighboring vertices to the adjacent vertices. This means that vertices with the
same distance from the starting vertex are visited before vertices further away
from the starting vertex are visited.
Steps:
Depth First Search is said to go "deep" because it visits a vertex, then an adjacent
vertex, and then that vertex' adjacent vertex, and so on, and in this way the
distance from the starting vertex increases for each recursive iteration.
Steps
Graph Traversal
1.BFS
2.DFS
Applications of Breadth First Search:
Shortest Path and Minimum Spanning Tree for unweighted graph:
Minimum Spanning Tree for weighted graphs:
Peer-to-Peer Networks
Crawlers in Search Engines:
Social Networking Websites
GPS Navigation systems
Broadcasting in Network
In Garbage Collection
Cycle detection in undirected graph:
Ford–Fulkerson algorithm
Path Finding
Finding all nodes within one connected component
Network Security
Connected Component
Topological sorting
Image processing:
Recommender systems:
Topological sorting in Directed Acyclic Graphs Social network analysis (finding the
(DAGs) shortest path between users)
Tree traversal (pre-order, in-order, post-order) Finding the shortest path in a maze
Conceptual BFS builds the tree level DFS builds the tree sub-tree
Difference by level. by sub-tree.
BFS is implemented using FIFO DFS is implemented using LIFO (Last In First
Principle
(First In First Out) principle. Out) principle.
DFS and BFS Time Complexity
Criteria BFS (Breadth-First Search) DFS (Depth-First Search)
Graph Type Applies to both directed and Applies to both directed and
undirected graphs undirected graphs
Best for Finding the shortest path in an Exploring all possible paths or
unweighted graph. solving puzzles like mazes.
Use Case - Shortest path in unweighted - Solving maze or puzzle
graphs (like social networks, problems.
web crawlers). - Detecting cycles in graphs,
- Level-order traversal of trees. topological sorting.
Performance on Deep May require more memory for Efficient for deep graphs, as it
Graphs wide/deep graphs, as the traverses a single path at a
queue holds many nodes. time.
Performance on Wide Efficient for shallow/wide May require more memory for
Graphs graphs, as it processes level by wide graphs, as the recursion
level. stack can grow large.
Shortest Path Guaranteed to find the shortest Not guaranteed to find the
path in an unweighted graph. shortest path, but explores all
paths.
Pathfinding in Not suitable for weighted Not suitable for weighted graphs
Weighted Graphs graphs. (Dijkstra's or Bellman-Ford
should be used).
Cycle Detection Can detect cycles, but less Useful for detecting cycles in
commonly used for it. directed or undirected graphs.
Graph Representation Can be used with both Can be used with both
adjacency lists and matrices. adjacency lists and matrices.
Tree Traversal Used for level-order traversal in Used for pre-order, in-order, and
trees. post-order traversal in trees.
Example of Use Finding the shortest path from Exploring all possible paths to
a source to all nodes in an solve a maze
unweighted graph.
Neither DFS nor BFS is inherently faster than the other; both have the same time
complexity of O(V + E), where V is the number of vertices and E is the number
of edges.
However, BFS is faster for finding the shortest path in unweighted graphs, while
DFS can be faster when searching deeper paths without concern for the shortest
path. The speed depends on the specific problem and the structure of the graph.
For the shortest path in an unweighted graph, BFS is the better choice because it
explores nodes level by level, ensuring the shortest path is found. DFS does not
guarantee the shortest path, as it explores one path deeply before backtracking,
which may result in longer paths being found first.