Graphs Notes
Graphs Notes
1. Introduction to Graphs
Basic Terminology:
• Vertex (Node): Fundamental unit of a graph,
representing entities.
• Edge (Link): Connection between two
vertices. Can be directed or undirected.
• Degree: Number of edges incident to a vertex.
For directed graphs, distinguish between in-
degree (edges coming in) and out-degree
(edges going out).
• Path: A sequence of vertices where each
adjacent pair is connected by an edge.
• Cycle: A path that starts and ends at the same
vertex with no other repetitions of vertices or
edges.
Types of Graphs:
• Directed Graph (Digraph): Edges have a
direction, going from one vertex to another.
• Undirected Graph: Edges have no direction;
the connection between vertices is
bidirectional.
• Weighted Graph: Edges have weights (values)
associated with them, representing costs or
distances.
• Unweighted Graph: Edges do not have
weights; all edges are considered equal.
2. Graph Representations
Adjacency Matrix:
• Definition: A 2D array A where A[i][j] is non-
zero if there is an edge between vertex i and
vertex j. For an undirected graph, the matrix is
symmetric.
• Space Complexity: O(V²), where V is the
number of vertices.
• Use Case: Efficient for dense graphs and
checking edge existence.
Adjacency List:
• Definition: An array of lists where each list
corresponds to a vertex and contains the
vertices adjacent to it.
• Space Complexity: O(V + E), where E is the
number of edges.
• Use Case: Efficient for sparse graphs and
iterating over neighbors.
3. Graph Traversals
Depth-First Search (DFS):
• Algorithm: Explore as far as possible along
each branch before backtracking.
Implemented using recursion or a stack.
• Applications: Topological sorting, solving
puzzles, pathfinding.
• Time Complexity: O(V + E), where V is the
number of vertices and E is the number of
edges.
Breadth-First Search (BFS):
• Algorithm: Explore all neighbors of a vertex
before moving to the next level. Implemented
using a queue.
• Applications: Finding the shortest path in an
unweighted graph, level-order traversal.
• Time Complexity: O(V + E).
4. Applications
Shortest Path Algorithms:
• Dijkstra’s Algorithm:
o Definition: Finds the shortest path from a
source vertex to all other vertices in a
weighted graph with non-negative
weights.
o Time Complexity: O(V²) with simple
implementation, O((V + E) log V) with
priority queue.
• Floyd-Warshall Algorithm:
o Definition: Finds shortest paths between
all pairs of vertices in a weighted graph.
o Time Complexity: O(V³).
o Use Case: Useful for dense graphs and
when all-pairs shortest paths are needed.
Minimum Spanning Tree (MST) Algorithms:
• Kruskal’s Algorithm:
o Definition: Constructs an MST by sorting
all edges and adding them one by one if
they don’t form a cycle (using a union-find
data structure).
o Time Complexity: O(E log E) or O(E log V)
depending on the implementation.
• Prim’s Algorithm:
o Definition: Builds an MST by starting from
a vertex and expanding the tree one edge
at a time.
o Time Complexity: O(E log V) with priority
queue, O(V²) with adjacency matrix.