CVR C O LLEG E OF
ENGINEERING
Name : M YASHODAR
Roll no : 24B81A0329
Branch : Mechanical
Subject : DSC Theory Activity
Topic: Explain Graph Representation -
Adjacency lists with example program
Agenda
Graph Representation: Adjacency Lists
Introduction to Graph Representation
Adjacency List Defined
Adjacency List Implementation
Building an Adjacency List
Example Graph and Adjacency List
Advantages of Adjacency Lists
Conclusion
Graph Representation:
Adjacency Lists
This presentation explores adjacency lists. We
will cover their storage and implementation.
We'll also examine their advantages in data
structures.
Introduction to Graph Representation
Graphs Fundamentals Representation Methods
Graphs consist of nodes (vertices) and Adjacency matrices and adjacency lists are
connections (edges). They model common methods. Adjacency lists are
relationships in various domains. efficient for sparse graphs.
• Social networks
• Routing paths
• System dependencies
Adjacency List Defined
Structure Directed Graphs
It's an array of lists. Each For directed graphs, the
list stores adjacent nodes. list contains only
Vertex 'i' has a list of its outgoing neighbors. This
connected vertices. shows the flow of
connections.
Undirected Graphs
In undirected graphs, the list includes all neighbors.
Connections are bidirectional.
Adjacency List Implementation
# Python Example
graph = {
'A': ['B', 'C'],
'B': ['A', 'D'],
'C': ['A'],
'D': ['B']
}
// C++ Example
vector> adj(n);
adj[0].push_back(1);
/* Java Example */
Building an Adjacency List
Add Neighbors
For each edge (u, v), add v
to u's list. Add u to v's list
for undirected graphs.
Provide Edges
Start with a list of graph Calculate Complexity
edges, like (A, B) and (A,
C). The process has a time
complexity of O(E), where
E is the number of edges.
Example Graph and Adjacency List
A:
[B, C]
B: Consider a graph with nodes A, B, C, D
[A, D]
and edges (A, B), (A, C), and (B, D). The
C: adjacency list represents these connections
[A]
clearly.
D:
[B]
Advantages of Adjacency Lists
Space Efficiency Neighbor Iteration
Iterating over a vertex's
Lists use O(V + E) space. neighbors is very efficient.
Matrices use O(V^2). Lists Only connected nodes are
are more compact for sparse checked.
graphs. Iterating
Large Graph Suitability
Ideal for large, sparse
graphs. This is where the
number of edges is much
less than V^2.
Conclusion
Versatile
Adjacency lists are a versatile graph representation.
Implementable
They are space-efficient and easy to implement.
Crucial
Essential for graph algorithms and network analysis.