Types of Graphs in Data Structures
Types of Graphs in Data Structures
There are different types of graphs in data structures, each of which is detailed below.
1. Finite Graph
The graph G=(V, E) is called a finite graph if the number of vertices and edges in the
graph is limited in number
2. Infinite Graph
The graph G=(V, E) is called a finite graph if the number of vertices and edges in the
graph is interminable.
3. Trivial Graph
If each pair of nodes or vertices in a graph G=(V, E) has only one edge, it is a simple
graph. As a result, there is just one edge linking two vertices, depicting one-to-one
interactions between two elements.
5. Multi Graph
If there are numerous edges between a pair of vertices in a graph G= (V, E), the graph is
referred to as a multigraph. There are no self-loops in a Multigraph.
6. Null Graph
It's a reworked version of a trivial graph. If several vertices but no edges connect them,
a graph G= (V, E) is a null graph.
7. Complete Graph
If a graph G= (V, E) is also a simple graph, it is complete. Using the edges, with n
number of vertices must be connected. It's also known as a full graph because each
vertex's degree must be n-1.
8. Pseudo Graph
If a graph G= (V, E) is a simple graph with the same degree at each vertex, it is a regular
graph. As a result, every whole graph is a regular graph.
A graph G= (V, E) is called a labeled or weighted graph because each edge has a value
or weight representing the cost of traversing that edge.
11. Directed Graph
An undirected graph comprises a set of nodes and links connecting them. The order of
the two connected vertices is irrelevant and has no direction. You can form an
undirected graph with a finite number of vertices and edges.
If there is a path between one vertex of a graph data structure and any other vertex, the
graph is connected.
14. Disconnected Graph
When there is no edge linking the vertices, you refer to the null graph as a disconnected
graph.
15. Cyclic Graph
It's also known as a directed acyclic graph (DAG), and it's a graph with directed edges
but no cycle. It represents the edges using an ordered pair of vertices since it directs the
vertices and stores some data.
18. Subgraph
The vertices and edges of a graph that are subsets of another graph are known as a
subgraph.
After you learn about the many types of graphs in graphs in data structures, you will
move on to graph terminologies.
• An edge is one of the two primary units used to form graphs. Each edge has
two ends, which are vertices to which it is attached.
• If two vertices are endpoints of the same edge, they are adjacent.
• A vertex's outgoing edges are directed edges that point to the origin.
• A vertex's incoming edges are directed edges that point to the vertex's
destination.
• A path is a set of alternating vertices and edges, with each vertex connected
by an edge.
• The path that starts and finishes at the same vertex is known as a cycle.
• A directed graph is weakly connected if all of its directed edges are replaced
with undirected edges, resulting in a connected graph. A weakly linked graph's
vertices have at least one out-degree or in-degree.
• A tree is a connected forest. The primary form of the tree is called a rooted
tree, which is a free tree.
• A spanning subgraph that is also a tree is known as a spanning tree.
The process of visiting or updating each vertex in a graph is known as graph traversal.
The sequence in which they visit the vertices is used to classify such traversals. Graph
traversal is a subset of tree traversal.
• Breadth-first search
• Depth-first search
BFS is a search technique for finding a node in a graph data structure that meets a set
of criteria.
• It begins at the root of the graph and investigates all nodes at the current
depth level before moving on to nodes at the next depth level.
• To maintain track of the child nodes that have been encountered but not yet
inspected, more memory, generally you require a queue.
Step 2: Select any vertex in your graph, say v1, from which you want to traverse the
graph.
Step 3: Examine any two data structures for traversing the graph.
Step 4: Starting from the vertex, you will add to the visited array, and afterward, you will
v1's adjacent vertices to the queue data structure.
Step 5: Now, using the FIFO concept, you must remove the element from the queue, put
it into the visited array, and then return to the queue to add the adjacent vertices of the
removed element.
Step 6: Repeat step 5 until the queue is not empty and no vertex is left to be visited.
DFS is a search technique for finding a node in a graph data structure that meets a set
of criteria.
Step 2: Select any vertex in our graph, say v1, from which you want to begin traversing
the graph.
Step 3: Examine any two data structures for traversing the graph.
Step 4: Insert v1 into the array's first block and push all the adjacent nodes or vertices of
vertex v1 into the stack.
Step 5: Now, using the FIFO principle, pop the topmost element and put it into the
visited array, pushing all of the popped element's nearby nodes into it.
Step 6: If the topmost element of the stack is already present in the array, discard it
instead of inserting it into the visited array.
Step 7: Repeat step 6 until the stack data structure isn't empty.
You will now look at applications of graph data structures after understanding the graph
traversal algorithm in this tutorial.
• Users on Facebook are referred to as vertices, and if they are friends, there is
an edge connecting them. The Friend Suggestion system on Facebook is
based on graph theory.
• You come across the Resource Allocation Graph in the Operating System,
where each process and resource are regarded vertically. Edges are drawn
from resources to assigned functions or from the requesting process to the
desired resource. A stalemate will develop if this results in the establishment
of a cycle.
• Web pages are referred to as vertices on the World Wide Web. Suppose there
is a link from page A to page B that can represent an edge. This application is
an illustration of a directed graph.
• Graph transformation systems manipulate graphs in memory using rules.
Graph databases store and query graph-structured data in a transaction-safe,
permanent manner.