0% found this document useful (0 votes)
1 views

Module 5 Graphs

The document provides an overview of graph terminology, including definitions of key concepts such as vertices, edges, paths, and cycles, as well as classifications like directed, undirected, weighted, and unweighted graphs. It also discusses graph representations through adjacency matrices and lists, along with traversal methods like Depth First Search (DFS) and Breadth First Search (BFS). Additionally, it covers applications such as topological sorting for Directed Acyclic Graphs (DAGs).

Uploaded by

Ratish Patil
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

Module 5 Graphs

The document provides an overview of graph terminology, including definitions of key concepts such as vertices, edges, paths, and cycles, as well as classifications like directed, undirected, weighted, and unweighted graphs. It also discusses graph representations through adjacency matrices and lists, along with traversal methods like Depth First Search (DFS) and Breadth First Search (BFS). Additionally, it covers applications such as topological sorting for Directed Acyclic Graphs (DAGs).

Uploaded by

Ratish Patil
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Introduction

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.

Basic Graph Terminology:


1. Graph
A Graph G is a non-empty set of vertices (or nodes) V and a set of edges E, where each edge
connects a pair of vertices. Formally, a graph can be represented as G= (V, E). Graphs can be
classified based on various properties, such as directedness of edges and connectivity.

2. Vertex (Node)

A Vertex, often referred to as a Node, is a fundamental unit of a graph. It represents an entity


within the graph. In applications like social networks, vertices can represent individuals, while in
road networks, they can represent intersections or locations.

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.

Advanced Graph Terminology:

1. Directed Graph (Digraph):

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

A Tree is a connected graph with no cycles. It is a fundamental data structure in


computer science, commonly used in algorithms like binary search trees and heap data
structures. Trees have properties such as a single root node, parent-child relationships
between nodes, and a unique path between any pair of nodes.

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.

• If there is an edge from vertex i to j, mark adjMat[i][j] as 1.


• If there is no edge from vertex i to j, mark adjMat[i][j] as 0.

Representation of Undirected Graph as Adjacency Matrix:

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].

Directed Graph to Adjacency Matrix

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 (

• Start at 1: Mark as visited. Output: 1


• Move to 2: Mark as visited. Output: 2
• Move to 0: Mark as visited. Output: 0 (backtrack to 2)
• Move to 3: Mark as visited. Output: 3 (backtrack to 2)
• Move to 4: Mark as visited. Output: 4 (backtrack to 1)
Breadth First Search (BFS):
Graph Application Topological Sorting
Topological sorting for Directed Acyclic Graph (DAG) is a linear ordering of vertices such that
for every directed edge u-v, vertex u comes before v in the ordering.
Note: Topological Sorting for a graph is not possible if the graph is not a DAG.
Example:
Input: Graph:

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”.

You might also like