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

Graphs

A graph is a non-linear data structure consisting of nodes connected by edges. The nodes are also called vertices. A graph can be represented using an adjacency matrix or adjacency list. Common applications that use graph data structures include social networks like Facebook, navigation systems like Google Maps, and web search algorithms.

Uploaded by

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

Graphs

A graph is a non-linear data structure consisting of nodes connected by edges. The nodes are also called vertices. A graph can be represented using an adjacency matrix or adjacency list. Common applications that use graph data structures include social networks like Facebook, navigation systems like Google Maps, and web search algorithms.

Uploaded by

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

What Are Graphs in Data Structure?

A graph is a non-linear kind of data structure made up of nodes or vertices and edges. The edges connect any two nodes
in the graph, and the nodes are also known as vertices.

This graph has a set of vertices V= { 1,2,3,4,5} and a set of


edges E= { (1,2),(1,3),(2,3),(2,4),(2,5),(3,5),(4,50 }.
Graph Terminology
1. Path

In the above graph, the path from 'a' to 'e' is


= {a,b,c,d,e}
2. Closed Path:

The above graph have a closed path, where the initial


node = {e} is same as the final node = {e}. So, the path
becomes = {e,d,f,g,e}.
3. Simple Path

In this example, {a,b,c,d}a,b,c,d is a simple


path. Because, this graph do not have any
loop or cycle and none of the paths point to
themselves.
4. Degree of a Node:

• Degree of a node is the number of edges connecting the node in


the graph. A simple example would be, suppose in facebook, if
you have 100 friends then the node that represents you has a
degree of 100.
Types of Graphs in Data Structures

• 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.
Representation of Graphs in Data Structures

• Graphs in data structures are used to represent the relationships between objects.
Every graph consists of a set of points known as vertices or nodes connected by
lines known as edges. The vertices in a network represent entities.

The most frequent graph representations are the two that follow:
•Adjacency matrix
•Adjacency list
Adjacency Matrix

• A sequential representation is an adjacency matrix.


• It's used to show which nodes are next to one another. I.e., is there
any connection between nodes in a graph?
• You create an MXM matrix G for this representation. If an edge exists
between vertex a and vertex b, the corresponding element of G, gi,j =
1, otherwise gi,j = 0.
• If there is a weighted graph, you can record the edge's weight instead
of 1s and 0s.
Undirected Graph Representation
Directed Graph Representation
Weighted Undirected Graph Representation

• Weight or cost is indicated at the graph's edge, a weighted graph


representing these values in the matrix.
Weighted Undirected Graph Representation Using Linked-List
Weighted Undirected Graph Representation Using an Array
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.

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

 Google Maps is another application that makes use of graphs. In the case of
Google Maps, each place is referred to as a node, and the roads that connect
them are referred to as edges.
• GPS systems and Google Maps use graphs to find the shortest path
from one destination to another.
• The Google Search algorithm uses graphs to determine the
relevance of search results.
• World Wide Web is the biggest graph. All the links and hyperlinks
are the nodes and their interconnection is the edges. This is why we
can open one webpage from the other.
•The nodes we represent in our graphs can be considered as the buildings, people,
group, landmarks or anything in general , whereas the edges are the paths
connecting them.

Just like in the below image, egdes are the roadways / path connecting the
nodes(like people, buildings, transports, etc).

You might also like