Graph Data Structure Comprehensive Presentation
Graph Data Structure Comprehensive Presentation
•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
1. Null 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.
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.
•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.
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;
int main()
{
vector<vector<int>> mat = {{ 0, 1, 0, 0 },
{ 1, 0, 1, 0 },
{ 0, 1, 0, 1 },
{ 0, 0, 1, 0 } }; */
return 0;
Adjacency List Representation of Graph:
// Main function
int main() {
// Create a graph with 4 vertices and no edges
int V = 4;
vector<vector<int>> adj(V);
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.
•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.