Presented by
M Usman(15434)
Muneeb Ahmad(15435)
M Saad(15432)
Taha Saeed(15433)
NaheemUllah(15436)
Graph data structure
What is a Graph?
• graph is a collection of nodes(vertices) and edges
connecting pairs of nodes.
• It models relationship between objects.
• Graphs can represent networks such as social connections,
roads or computer networks.
Types of graph
• Directed: Edges have direction (A → B).
• Undirected: Edges have no direction (A —
B).
• Weighted: Edges have values like cost or
distance.
• Unweighted: All edges are equal.
Graph representation
graph representation refers to how graph is
stored in memory.
• Adjacency matrix: A 2D table where each cell
shows if there is a connection between two
nodes.
• (1=connected , 0=not connected)
Adjacency matrix
Adjacency list
• Each vertex stores a list of its directly
connected vertices(neighbours).
• Memory efficient especially for sparse
graph(graph with few edges).
Adjacency list
Graph traversal algorithm
• A method to visit all nodes in a graph
systematically.
• Helps explore or search the graph’s structure.
• Used in tasks like finding paths,checking
connectivity and searching.
Graph traversal algorthm
Depth first search(DFS)
• A graph traversal method that explores deep
into each path before backtracking.
• Starts from a selected node and visits
unvisited neighbours recursively.
• Continues until all reachable nodes are
visited.
Graph traversal algorithm
DFS(algorithm)
• Start from a node.
• Mark the node as visited.
• Go to the next unvisited neighbor.
• Repeat the process for each new node.
• If no more neighbors, go back (backtrack) and
try others.
Graph traversal algorithm
Breadth first search(BFS)
• Breadth First Search (BFS) visits all nodes
level by level, starting from a node and
exploring its neighbors before moving deeper.
Graph traversal algorithm
BFS (algorithm)
• Start with the first node; put it in a queue.
• Mark this node as visited.
• Repeat until the queue is empty:
a. Take the front node from the queue.
b. Look at all its neighbors.
c. For each neighbor not visited yet:
- Mark it visited.
- Add it to the queue.
Graph traversal algorithm
Applications of graph data structure
• Social networks
• Web page links
• Transportation routes
• Network data routing
• Recommendations
• Task scheduling
• Circuit design
• Game pathfinding