Module 5 Graphs
Module 5 Graphs
Graphs are fundamental data structures in various computer science applications, including
network design, social network analysis, and route planning. Understanding graph terminology is
crucial for effectively navigating and manipulating graph data structures.
Graph Terminologies
Graph terminology is important for understanding and communicating about relationships and
connections in data. It provides a common language for describing the components of a graph,
such as vertices (nodes) and edges (connections). This clarity helps in problem-solving, algorithm
design, data representation, and collaboration.
2. Vertex (Node)
3. Edge
An Edge is a connection between two vertices in a graph. It can be either directed or undirected.
In a directed graph, edges have a specific direction, indicating a one-way connection between
vertices. In contrast, undirected graphs have edges that do not have a direction and represent
bidirectional connections.
4. Degree of a Vertex
The Degree of a Vertex in a graph is the number of edges incident to that vertex. In a directed
graph, the degree is further categorized into the in-degree (number of incoming edges) and out-
degree (number of outgoing edges) of the vertex.
5. Path
A Path in a graph is a sequence of vertices where each adjacent pair is connected by an edge.
Paths can be of varying lengths and may or may not visit the same vertex more than once. The
shortest path between two vertices is of particular interest in algorithms such as Dijkstra’s
algorithm for finding the shortest path in weighted graphs.
6. Cycle
A Cycle in a graph is a path that starts and ends at the same vertex, with no repetitions of
vertices (except the starting and ending vertex, which are the same). Cycles are essential in
understanding the connectivity and structure of a graph and play a significant role in cycle
detection algorithms.
A Directed Graph consists of nodes (vertices) connected by directed edges (arcs). Each edge has
a specific direction, meaning it goes from one node to another. Directed Graph is a network
where information flows in a specific order. Examples include social media follower
relationships, web page links, and transportation routes with one-way streets.
2. Undirected Graph:
In an Undirected Graph, edges have no direction. They simply connect nodes without any
inherent order. For example, a social network where friendships exist between people, or a map
of cities connected by roads (where traffic can flow in both directions).
3. Weighted Graph:
Weighted graphs assign numerical values (weights) to edges. These weights represent some
property associated with the connection between nodes. For example, road networks with
varying distances between cities, or airline routes with different flight durations, are examples of
weighted graphs.
4. Unweighted Graph:
An unweighted graph has no edge weights. It focuses solely on connectivity between nodes. For
example: a simple social network where friendships exist without any additional information, or
a family tree connecting relatives.
5. Connected Graph:
A graph is connected if there is a path between any pair of nodes. In other words, you can reach
any node from any other node. Even a single-node graph is considered connected. For larger
graphs, there’s always a way to move from one node to another.
6. Acyclic Graph:
An acyclic graph contains no cycles (closed loops). In other words, you cannot start at a node
and follow edges to return to the same node. Examples include family trees (without marriages
between relatives) or dependency graphs in software development.
7. Cyclic Graph:
A cyclic graph has at least one cycle. You can traverse edges and eventually return to the same
node. For example: circular road system or a sequence of events that repeats indefinitely.
8. Connected Graph
A Graph is connected if there is a path between every pair of vertices in the graph. In
a directed graph, the concept of strong connectivity refers to the existence of a directed
path between every pair of vertices.
9. Disconnected Graph:
A disconnected graph has isolated components that are not connected to each other.
These components are separate subgraphs.
10. Tree
Representations of Graph
Here are the two most common ways to represent a graph : For simplicity, we are going to
consider only unweighted graphs in this post.
1. Adjacency Matrix
2. Adjacency List
Adjacency Matrix
An adjacency matrix is a way of representing a graph as a matrix of boolean (0’s and 1’s)
Let’s assume there are n vertices in the graph So, create a 2D matrix adjMat[n][n] having
dimension n x n.
The below figure shows an undirected graph. Initially, the entire Matrix is initialized to 0. If
there is an edge from source to destination, we insert 1 to both cases (adjMat[destination] and
adjMat[destination]) because we can go either way.
Representation of Directed Graph as Adjacency Matrix:
The below figure shows a directed graph. Initially, the entire Matrix is initialized to 0.
If there is an edge from source to destination, we insert 1 for that particular
adjMat[destination].
Adjacency List
An array of Lists is used to store edges between two vertices. The size of array is equal to the
number of vertices (i.e, n). Each index in this array represents a specific vertex in the graph. The
entry at the index i of the array contains a linked list containing the vertices that are adjacent to
vertex i.
Let’s assume there are n vertices in the graph So, create an array of list of size n as adjList[n].
• adjList[0] will have all the nodes which are connected (neighbour) to vertex 0.
• adjList[1] will have all the nodes which are connected (neighbour) to vertex 1 and so on
Graph Traversals Depth First Search (DFS) and Breadth First Search (BFS)
Depth First Traversal (or DFS) for a graph is similar to Depth First Traversal of a tree. Like
Trees, we traverse all adjacent one by one, when we traverse an adjacent, we finish traversal of
all vertices reachable through the adjacent completely. After we finish one adjacent and its
reachable, we go to the next adjacent and finish all reachable through next and continue this way.
Similar to tree where we first completely traverse the left subtree and then go to the right subtree.
The only catch here is, that, unlike trees, graphs may contain cycles (a node may be visited
twice). To avoid processing a node more than once, use a boolean visited array
Example:
Input: V = 5, E = 5, edges = {{1, 2}, {1, 0}, {0, 2}, {2, 3}, {2, 4}}, s = 1
Output: 1 2 0 3 4
Explanation: The source vertex s is 1. We visit it first, then we visit an adjacent. There are two
adjacent 1, 0 and 2. We can pick any of the two (
Output: 5 4 2 3 1 0
Explanation: The first vertex in topological sorting is always a vertex with an in-degree of 0 (a
vertex with no incoming edges). A topological sorting of the following graph is “5 4 2 3 1 0”.
There can be more than one topological sorting for a graph. Another topological sorting of the
following graph is “4 5 2 3 1 0”.