0% found this document useful (0 votes)
164 views18 pages

Types of Graphs in Data Structures

The document discusses different types of graphs in data structures, including finite graphs, infinite graphs, trivial graphs, simple graphs, multi graphs, null graphs, complete graphs, pseudo graphs, regular graphs, weighted graphs, directed graphs, undirected graphs, connected graphs, disconnected graphs, cyclic graphs, acyclic graphs, directed acyclic graphs, and subgraphs. It then covers graph terminologies and traversal algorithms like breadth-first search and depth-first search before discussing applications of graphs in data structures such as social networks, operating systems, and the world wide web.

Uploaded by

Jahangir Ali
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
164 views18 pages

Types of Graphs in Data Structures

The document discusses different types of graphs in data structures, including finite graphs, infinite graphs, trivial graphs, simple graphs, multi graphs, null graphs, complete graphs, pseudo graphs, regular graphs, weighted graphs, directed graphs, undirected graphs, connected graphs, disconnected graphs, cyclic graphs, acyclic graphs, directed acyclic graphs, and subgraphs. It then covers graph terminologies and traversal algorithms like breadth-first search and depth-first search before discussing applications of graphs in data structures such as social networks, operating systems, and the world wide web.

Uploaded by

Jahangir Ali
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 18

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

A graph G= (V, E) is trivial if it contains only a single vertex and no edges.


4. Simple 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) contains a self-loop besides other edges, it is a pseudograph.


9. Regular 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.

10. Weighted 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

A directed graph also referred to as a digraph, is a set of nodes connected by edges,


each with a direction.
12. Undirected 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.

13. Connected Graph

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

If a graph contains at least one graph cycle, it is considered to be cyclic.

16. Acyclic Graph

When there are no cycles in a graph, it is called an acyclic graph.


17. Directed Acyclic 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.

Terminologies of Graphs in Data Structures

Following are the basic terminologies of graphs in data structures:

• 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.

• The total number of edges occurring to a vertex in a graph is its degree.

• The out-degree of a vertex in a directed graph is the total number of outgoing


edges, whereas the in-degree is the total number of incoming edges.

• A vertex with an in-degree of zero is referred to as a source vertex, while one


with an out-degree of zero is known as sink vertex.

• An isolated vertex is a zero-degree vertex that is not an edge's endpoint.

• 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 path with unique vertices is called a simple path.

• For each pair of vertices x, y, a graph is strongly connected if it contains a


directed path from x to y and a directed path from y to x.

• 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.

• A connected component is the unconnected graph's most connected


subgraph.

• A bridge, which is an edge of removal, would sever the graph.

• Forest is a graph without a cycle.

Graph Traversal Algorithm

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.

There are two techniques to implement a graph traversal algorithm:

• Breadth-first search

• Depth-first search

Breadth-First Search or BFS

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.

Algorithm of breadth-first search

Step 1: Consider the graph you want to navigate.

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.

• Visited array (size of the graph)

• Queue data structure

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.

Depth-First Search or DFS

DFS is a search technique for finding a node in a graph data structure that meets a set
of criteria.

• The depth-first search (DFS) algorithm traverses or explores data structures


such as trees and graphs. The DFS algorithm begins at the root node and
examines each branch as far as feasible before backtracking.
• To maintain track of the child nodes that have been encountered but not yet
inspected, more memory, generally a stack, is required.

Algorithm of depth-first search

Step 1: Consider the graph you want to navigate.

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.

• Visited array (size of the graph)

• Stack data structure

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.

Application of Graphs in Data Structures

Following are some applications of graphs in data structures:

• Graphs are used in computer science to depict the flow of computation.

• 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.

You might also like