Unit 4 of 4 · M.Sc IT Sem 2

Unit 4: Graphs

Data Structures notes · PTU syllabus (PGCA1913)

3 min read7 topics10 exam questions
On this page
  1. Unit summary
  2. Graph terminology
  3. Types of graphs
  4. Adjacency matrix and adjacency list
  5. Adjacency multilist
  6. Depth-first and breadth-first traversal
  7. Minimum spanning trees and Kruskal's algorithm
  8. Dijkstra's shortest path algorithm
  9. Key terms
  10. Quick revision
  11. Important questions

Unit summary

Graphs model networks of roads, computers and relationships. This unit covers graph representations — adjacency matrix, adjacency list and adjacency multilist — depth-first and breadth-first traversal, minimum spanning trees with Kruskal's algorithm, and shortest paths with Dijkstra's algorithm.

After this unit you can

  • Represent graphs in memory
  • Traverse graphs with DFS and BFS
  • Find minimum spanning trees with Kruskal's algorithm
  • Find shortest paths with Dijkstra's algorithm

PTU syllabus topics

  • Graph representations (adjacency matrix, adjacency list, adjacency multilist)
  • depth-first and breadth-first traversal
  • minimum spanning tree
  • shortest path algorithms — Kruskal's and Dijkstra's
ComparisonBFS vs DFS
BFS
DFS

Uses

Queue

Stack or recursion

Explores

Level by level

As deep as possible first

Finds

Shortest path in unweighted graphs

Cycles, topological order

Memory

Can be high for wide graphs

Lower for wide graphs

1

Topic 1

Graph terminology

A graph G = (V, E) is a set of vertices (nodes) V and a set of edges E connecting pairs of vertices.

Key termsGraph vocabulary
Adjacent vertices
Vertices joined by an edge
Degree
Number of edges at a vertex; in-degree and out-degree for directed graphs
Path
A sequence of vertices joined by edges
Cycle
A path that starts and ends at the same vertex
Connected graph
A path exists between every pair of vertices
Weighted graph
Edges carry values such as distance or cost
2

Topic 2

Types of graphs

TypeMeaning
UndirectedEdges have no direction (A–B same as B–A)
Directed (digraph)Edges have direction (A → B)
WeightedEdges have weights or costs
CompleteEvery vertex is joined to every other vertex
Cyclic / acyclicContains a cycle / contains no cycle
Simple / multigraphNo loops or parallel edges / allows parallel edges

A tree is a connected acyclic graph.

3

Topic 3

Adjacency matrix and adjacency list

ComparisonGraph representations
Adjacency matrix
Adjacency list

Structure

V × V array; A[i][j] = 1 or the weight if an edge exists

Array of V lists of neighbours

Space

O(V²)

O(V + E)

Edge check

O(1)

O(degree)

Suits

Dense graphs

Sparse graphs

Example

Undirected graph with edges A–B, A–C, B–D, C–D: matrix rows A: 0 1 1 0, B: 1 0 0 1, C: 1 0 0 1, D: 0 1 1 0; lists A → B, C; B → A, D; C → A, D; D → B, C.

4

Topic 4

Adjacency multilist

  • In an adjacency list of an undirected graph each edge appears twice. An adjacency multilist stores each edge once as a node shared by both vertex lists.
Key termsMultilist edge node
Mark bit
Whether the edge has been visited
Vertex 1 and vertex 2
The two endpoints
Link 1
Next edge incident on vertex 1
Link 2
Next edge incident on vertex 2
Weight (optional)
Cost of the edge
  • Useful when edges must be marked or deleted once — e.g., in algorithms that process each edge a single time.
5

Topic 5

Depth-first and breadth-first traversal

ComparisonDFS vs BFS
Depth-first search
Breadth-first search

Data structure

Stack (or recursion)

Queue

Explores

As deep as possible before backtracking

Level by level, nearest first

Finds

Cycles, connected components, topological order

Shortest path in an unweighted graph

Time

O(V + E)

O(V + E)

Example

Graph edges A–B, A–C, B–D, C–E. Starting from A: DFS visits A, B, D, C, E; BFS visits A, B, C, D, E.

Exam tip

Mark each vertex as visited when you first reach it; otherwise cycles cause infinite loops.

6

Topic 6

Minimum spanning trees and Kruskal's algorithm

  • A spanning tree of a connected graph contains all V vertices and V − 1 edges without cycles; a minimum spanning tree (MST) has the least total weight.
ProcessKruskal's algorithm
  1. 1Sort all edges by increasing weight
  2. 2Take the smallest remaining edge
  3. 3Add it if it does not form a cycle (union–find check)
  4. 4Repeat until V − 1 edges are chosen

Example

Edges A–B 1, B–C 2, A–C 3, C–D 4, B–D 5: choose A–B (1), B–C (2), skip A–C (cycle), choose C–D (4). MST weight = 7.

  • Complexity O(E log E). Prim's algorithm grows the tree from a start vertex, always adding the cheapest edge to a new vertex.
7

Topic 7

Dijkstra's shortest path algorithm

Dijkstra's algorithm finds the shortest distance from a source vertex to all other vertices in a weighted graph with non-negative edge weights.

ProcessDijkstra's algorithm
  1. 1Initialise

    dist[source] = 0, all others ∞

  2. 2Pick the unvisited vertex u with the smallest dist
  3. 3Relax each neighbour v

    If dist[u] + w(u, v) < dist[v], update dist[v]

  4. 4Mark u visited
  5. 5Repeat until all vertices are visited

Example

From A: A–B = 4, A–C = 1, C–B = 2, B–D = 1. dist: A = 0, C = 1, then B = min(4, 1 + 2) = 3, then D = 3 + 1 = 4.

With a simple array the algorithm takes O(V²); with a priority queue (heap), O((V + E) log V).

Key terms

Adjacency multilist
Representation storing each undirected edge once
DFS
Traversal going deep before backtracking, using a stack
BFS
Level-by-level traversal using a queue
Minimum spanning tree
Spanning tree of least total weight
Dijkstra's algorithm
Single-source shortest paths with non-negative weights

Quick revision

  • Vertices, edges, degree, path, cycle, connected; directed, weighted, complete graphs.
  • Matrix O(V²), list O(V + E), multilist edges stored once.
  • DFS with a stack; BFS with a queue.
  • Kruskal: sort edges, union–find; Prim.
  • Dijkstra: greedy, non-negative weights, O(V²) or O((V + E) log V) with a heap.

Important exam questions

Practice questions written to the PTU exam pattern for this unit's syllabus: short answers (Section A style) and long answers (Sections B and C style).

Short-answer questions

  1. Q1.Compare adjacency matrix and adjacency list storage.
  2. Q2.What is an adjacency multilist?
  3. Q3.Which data structure is used in BFS?
  4. Q4.How many edges does a spanning tree of 8 vertices have?
  5. Q5.How does Kruskal's algorithm avoid cycles?
  6. Q6.Why can't Dijkstra's algorithm handle negative weights?

Long-answer questions

  1. Q1.Explain graph representations with an example.
  2. Q2.Explain DFS and BFS with an example.
  3. Q3.Find the minimum spanning tree of a graph using Kruskal's algorithm.
  4. Q4.Find shortest paths using Dijkstra's algorithm (numerical).

Stuck on this unit?

Message SBS on WhatsApp for help with Data Structures, or to ask about studying M.Sc IT at Synetic.

WhatsApp us