Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

Graph Data Structure Comprehensive Presentation

Uploaded by

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

Graph Data Structure Comprehensive Presentation

Uploaded by

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

Graph Data Structure

Comprehensive Explanation with


Terms, Properties, and Applications
What is a Graph?
• A Graph is a non-linear data structure
consisting of vertices (nodes) and edges.
• Vertices represent entities, and edges
represent connections between them.
• Used to model relationships, networks, and
processes in various applications.
Graph Data Structure is a non-linear data structure consisting of vertices
and edges. It is useful in fields such as social network analysis,
recommendation systems, and computer networks. In the field of sports data
science, graph data structure can be used to analyze and understand the
dynamics of team performance and player interactions on the field.
What is Graph Data Structure?
Graph is a non-linear data structure consisting of vertices and edges. The
vertices are sometimes also referred to as nodes and the edges are lines or
arcs that connect any two nodes in the graph. More formally a Graph is
composed of a set of vertices( V ) and a set of edges( E ). The graph is
denoted by G(V, E).
Imagine a game of football as a web of connections, where players are the
nodes and their interactions on the field are the edges. This web of
connections is exactly what a graph data structure represents, and it’s the
key to unlocking insights into team performance and player dynamics in
sports.
Components of Graph Data Structure
•Vertices: Vertices are the fundamental units of the graph. Sometimes,
vertices are also known as vertex or nodes. Every node/vertex can be labeled
or unlabelled.

•Edges: Edges are drawn or used to connect two nodes of the graph. It can
be ordered pair of nodes in a directed graph. Edges can connect any two
nodes in any possible way. There are no rules. Sometimes, edges are also
known as arcs. Every edge can be labelled/unlabelled.
Mathematical Representation
• A graph is represented as G = (V, E):
• V - Set of vertices (nodes)
• E - Set of edges connecting pairs of vertices
• Example:
• V = {1, 2, 3, 4}
• E = {(1,2), (1,3), (2,4)}
Difference Between Graph and Tree

Graphs and trees are two fundamental data structures used in computer
science to represent relationships between objects. While they share some
similarities, they also have distinct differences that make them suitable for
different applications.
What is Graph?
A graph data structure is a collection of nodes (also called vertices)
and edges that connect them. Nodes can represent entities, such as people,
places, or things, while edges represent relationships between those entities.
Graphs are used to model a wide variety of real-world systems, such
as social networks, transportation networks, and computer networks.

What is Tree?
A tree data structure is a hierarchical data structure that consists of nodes
connected by edges. Each node can have multiple child nodes, but only one
parent node. The topmost node in the tree is called the root node.
Trees are often used to represent hierarchical data, such as file systems, XML
documents, and organizational charts.
Key Differences Between Graph and Tree

•Cycles: Graphs can contain cycles, while trees cannot.

•Connectivity: Graphs can be disconnected (i.e., have multiple


components), while trees are always connected.

•Hierarchy: Trees have a hierarchical structure, with one vertex designated


as the root. Graphs do not have this hierarchical structure.

•Applications: Graphs are used in a wide variety of applications, such as


social networks, transportation networks, and computer science. Trees are
often used in hierarchical data structures, such as file systems and XML
documents.
Types Of Graphs in Data Structure and Algorithms

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.

10. Cyclic Graph

A graph containing at least one cycle is known as a Cyclic graph.


11. Directed Acyclic Graph

A Directed Graph that does not contain any cycle.

12. Bipartite Graph

A graph in which vertex can be divided into two sets such that vertex in each
set does not contain any edge between them.
13. Weighted Graph

• A graph in which the edges are already specified with suitable weight is
known as a weighted graph.

• Weighted graphs can be further classified as directed weighted graphs and


undirected weighted graphs.
Representation of Graph Data Structure:
There are multiple ways to store a graph: The following are the most
common representations.
•Adjacency Matrix

•Adjacency List

•Adjacency Matrix Representation of Graph Data Structure:

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.
Below is the implementation of Graph Data Structure represented using Adjacency Matrix:
// C++ program to demonstrate Adjacency Matrix
// representation of undirected and unweighted graph
#include <bits/stdc++.h>
using namespace std;

void addEdge(vector<vector<int>> &mat, int i, int j)


{
mat[i][j] = 1;
mat[j][i] = 1; // Since the graph is undirected
}

void displayMatrix(vector<vector<int>> &mat)


{
int V = mat.size();
for (int i = 0; i < V; i++)
{
for (int j = 0; j < V; j++)
cout << mat[i][j] << " ";
cout << endl;
}
}

int main()
{

// Create a graph with 4 vertices and no edges


// Note that all values are initialized as 0
int V = 4;
vector<vector<int>> mat(V, vector<int>(V, 0));

// Now add edges one by one


addEdge(mat, 0, 1);
addEdge(mat, 0, 2);
addEdge(mat, 1, 2);
addEdge(mat, 2, 3);

/* Alternatively we can also create using below


code if we know all edges in advacem

vector<vector<int>> mat = {{ 0, 1, 0, 0 },
{ 1, 0, 1, 0 },
{ 0, 1, 0, 1 },
{ 0, 0, 1, 0 } }; */

cout << "Adjacency Matrix Representation" << endl;


displayMatrix(mat);

return 0;
Adjacency List Representation of Graph:

This graph is represented as a collection of linked lists. There is an array of


pointer which points to the edges connected to that vertex.
Below is the implementation of Graph Data Structure represented using Adjacency List:
#include <iostream>
#include <vector>
using namespace std;

// Function to add an edge between two vertices


void addEdge(vector<vector<int>>& adj, int i, int j) {
adj[i].push_back(j);
adj[j].push_back(i); // Undirected
}

// Function to display the adjacency list


void displayAdjList(const vector<vector<int>>& adj) {
for (int i = 0; i < adj.size(); i++) {
cout << i << ": "; // Print the vertex
for (int j : adj[i]) {
cout << j << " "; // Print its adjacent
}
cout << endl;
}
}

// Main function
int main() {
// Create a graph with 4 vertices and no edges
int V = 4;
vector<vector<int>> adj(V);

// Now add edges one by one


addEdge(adj, 0, 1);
addEdge(adj, 0, 2);
addEdge(adj, 1, 2);
addEdge(adj, 2, 3);

cout << "Adjacency List Representation:" << endl;


displayAdjList(adj);

return 0;
}
For exams, carefully review the operations listed below and also they are the last questions
of your assignment. Open the hyperlinks provided, as these represent the final 4 questions
of your assignment. You are required to complete all tasks to ensure thorough preparation
for your exam.
To simplify your assignment, combine the provided code and implement all operations within a
single question. This approach avoids repetitive coding and reinforces your understanding of the
concepts discussed in class. We have already covered the introduction in class, and now your task
is to practice and execute the operations independently.

Basic Operations on Graph Data Structure:


Below are the basic operations on the graph:
•Insertion or Deletion of Nodes in the graph

• Add and Remove vertex in Adjacency List representation of Graph


• Add and Remove vertex in Adjacency Matrix representation of Graph
•Insertion or Deletion of Edges in the graph

• Add and Remove Edge in Adjacency List representation of a Graph


• Add and Remove Edge in Adjacency Matrix representation of a Graph
•Searching in Graph Data Structure- Search an entity in the graph.

•Traversal of Graph Data Structure- Traversing all the nodes in the graph.
Graph Applications - Algorithms
• 1. Dijkstra’s Algorithm - Shortest path.
• 2. Prim’s Algorithm - Minimum spanning tree.
• 3. Kruskal’s Algorithm - Minimum spanning
tree.
• 4. Bellman-Ford Algorithm - Shortest paths
with negative weights.
Graph Applications - Real World
• 1. Social Networks - Modeling relationships
and connections.
• 2. Google Maps - Shortest path computation.
• 3. Web Crawlers - Searching and indexing
websites.
• 4. Communication Networks - Routing and
optimization.
• 5. Electrical Circuits - Modeling connections.
Properties of Graphs
• 1. Connected Graph - All vertices are
reachable.
• 2. Disconnected Graph - Some vertices are not
reachable.
• 3. Cyclic Graph - Contains cycles.
• 4. Acyclic Graph - No cycles present.
Summary
• Graphs model relationships and processes in
networks.
• Key concepts include paths, adjacency, cycles,
and degrees.
• Used widely in algorithms, networks, and
communication systems.

You might also like