You are on page 1of 12

Data Structures & Algorithms

Chapter # 7
Introduction to Graphs

By:
Muhammad Imran
Assistant Prof. of Computer Science
Govt. Millat Degree College Mumtazabad, Multan
Lecturer (Visiting Faculty) of I.T
University of Education (Multan Campus) Multan
Ch # 7 (Introduction To Graphs) 2

INTRODUCTION TO GRAPHS
Graph is a non linear data structure, it contains a set of points known as nodes (or
vertices) and set of links known as edges (or Arcs) which connects the vertices. A graph
is defined as follows..

Graph is a collection of nodes and edges which connects nodes in the graph.

Generally, a graph G is represented as G = ( V , E ), where V is set of vertices and E is set


of edges.

Example

The following is a graph with 5 vertices and 6 edges.

This graph G can be defined as G = ( V , E )

Where V = {A,B,C,D,E} and E = {(A,B),(A,C)(A,D),(B,D),(C,D),(B,E),(E,D)}.

Graph Terminologies
We use the following terms in graph data structure...

Vertex

A individual data element of a graph is called as Vertex. Vertex is also known as node. In
above example graph, A, B, C, D & E are known as vertices.

Edge

Prepared By: Muhammad Imran


Assistant Prof. of Computer Science, Govt. Millat Degree College, Multan.
Lecturer (Visiting Faculty) of I.T, University of Education Multan Campus.
Ch # 7 (Introduction To Graphs) 3

An edge is a connecting link between two vertices. Edge is also known as Arc. An edge is
represented as (startingVertex, endingVertex). For example, in above graph, the link
between vertices A and B is represented as (A,B). In above example graph, there are 7
edges (i.e., (A,B), (A,C), (A,D), (B,D), (B,E), (C,D), (D,E)).

Edges are three types.

1. Undirected Edge - An undirected egde is a bidirectional edge. If there is a


undirected edge between vertices A and B then edge (A , B) is equal to edge (B ,
A).
2. Directed Edge - A directed egde is a unidirectional edge. If there is a directed edge
between vertices A and B then edge (A , B) is not equal to edge (B , A).
3. Weighted Edge - A weighted egde is an edge with cost on it.

Types of Graphs
1. Undirected Graph

A graph with only undirected edges is said to be undirected graph.

2. Directed Graph

A graph with only directed edges is said to be directed graph.

Prepared By: Muhammad Imran


Assistant Prof. of Computer Science, Govt. Millat Degree College, Multan.
Lecturer (Visiting Faculty) of I.T, University of Education Multan Campus.
Ch # 7 (Introduction To Graphs) 4

Figure (a) and (d) are undirected graphs and (e) directed graph

The number of incoming edges to a vertex v is called in–degree of the vertex (denote
indeg(v)). The number of outgoing edges from a vertex is called out-degree (denote
outdeg(v)). For example, let us consider the digraph shown in figure e.

Indegree(v1) = 1 outdegree(v1) = 1
Indegree(v2) = 1 outdegree(v2) = 2
Indegree(v3) = 1 outdegree(v3) = 1
Indegree(v4) = 2 outdegree(v1) = 1

3. Weighted Graph

In many applications, each edge of a graph has an associated numerical value, called a
weight. Usually, the edge weights are nonnegative integers. Weighted graphs may be
either directed or undirected.

The weight of an edge is often referred to as the "cost" of the edge. In applications, the
weight may be a measure of the length of a route, the capacity of a line, the energy
required to move between locations along a route, etc.

Prepared By: Muhammad Imran


Assistant Prof. of Computer Science, Govt. Millat Degree College, Multan.
Lecturer (Visiting Faculty) of I.T, University of Education Multan Campus.
Ch # 7 (Introduction To Graphs) 5

4. Cyclic Graph

If there is a path containing one or more edges which starts from a vertex Vi and
terminates into the same vertex then the path is known as a cycle, and this type of graph
is known as Cyclic Graph. The following graphs are Cyclic Graphs.

5. Acyclic Graph

If a graph (digraph) does not have any cycle then it is called acyclic graph. For example,
the graphs of figure (f) and figure (g) are acyclic graphs.

Prepared By: Muhammad Imran


Assistant Prof. of Computer Science, Govt. Millat Degree College, Multan.
Lecturer (Visiting Faculty) of I.T, University of Education Multan Campus.
Ch # 7 (Introduction To Graphs) 6

Applications of Graphs

Since they are powerful abstractions, graphs can be very important in modeling data. In
fact, many problems can be reduced to known graph problems. Here we outline just some
of the many applications of graphs.

1. Social network graphs: to tweet or not to tweet. Graphs that represent who knows
whom, who communicates with whom, who influences whom or other relationships in
social structures. An example is the twitter graph of who follows whom.

2. Transportation networks

In road networks vertices are intersections and edges are the road segments between
them, and for public transportation networks vertices are stops and edges are the links
between them.

3. Document link graphs. The best known example is the link graph of the web, where
each web page is a vertex, and each hyperlink a directed edge.

4. Maps – finding the shortest/cheapest path for a car from one city to another, by using
given roads.

Graph Representations

Prepared By: Muhammad Imran


Assistant Prof. of Computer Science, Govt. Millat Degree College, Multan.
Lecturer (Visiting Faculty) of I.T, University of Education Multan Campus.
Ch # 7 (Introduction To Graphs) 7

Graph data structure is represented using following representations...

1. Adjacency Matrix
2. Incidence Matrix
3. Adjacency List

Adjacency Matrix
In this representation, graph can be represented using a matrix of size total number of
vertices by total number of vertices. That means if a graph with 4 vertices can be
represented using a matrix of 4X4 class. In this matrix, rows and columns both represents
vertices. This matrix is filled with either 1 or 0. Here, 1 represents there is an edge from
row vertex to column vertex and 0 represents there is no edge from row vertex to column
vertex.

For example, consider the following undirected graph representation...

Directed graph representation...

Prepared By: Muhammad Imran


Assistant Prof. of Computer Science, Govt. Millat Degree College, Multan.
Lecturer (Visiting Faculty) of I.T, University of Education Multan Campus.
Ch # 7 (Introduction To Graphs) 8

2. Incidence Matrix
In this representation, graph can be represented using a matrix of size total number of
vertices by total number of edges. That means if a graph with 4 vertices and 6 edges can
be represented using a matrix of 4X6 class. In this matrix, rows represents vertices and
columns represents edges. This matrix is filled with either 0 or 1 or -1. Here, 0 represents
row edge is not connected to column vertex, 1 represents row edge is connected as
outgoing edge to column vertex and -1 represents row edge is connected as incoming
edge to column vertex.

For example, consider the following directed graph representation...

3. Adjacency List
In this representation, every vertex of graph contains list of its adjacent vertices.

Prepared By: Muhammad Imran


Assistant Prof. of Computer Science, Govt. Millat Degree College, Multan.
Lecturer (Visiting Faculty) of I.T, University of Education Multan Campus.
Ch # 7 (Introduction To Graphs) 9

For example, consider the following directed graph representation implemented using
linked list...

This representation can also be implemented using array as follows..

Minimum Spanning Tree (MST):


A spanning tree for a connected graph is a tree whose vertex set is the same as the vertex
set of the given graph, and whose edge set is a subset of the edge set of the given graph.
i.e., any connected graph will have a spanning tree.
Weight of a spanning tree w(T) is the sum of weights of all edges in T. Minimum
spanning tree (MST) is a spanning tree with the smallest possible weight.
Example:

Prepared By: Muhammad Imran


Assistant Prof. of Computer Science, Govt. Millat Degree College, Multan.
Lecturer (Visiting Faculty) of I.T, University of Education Multan Campus.
Ch # 7 (Introduction To Graphs) 10

Let's consider a couple of real-world examples on minimum spanning tree:

• One practical application of a MST would be in the design of a network. For instance, a
group of individuals, who are separated by varying distances, wish to be connected
together in a telephone network. Although MST cannot do anything about the distance
from one connection to another, it can be used to determine the least cost paths with no
cycles in this network, thereby connecting everyone at a minimum cost.

• Another useful application of MST would be finding airline routes. The vertices of the
graph would represent cities, and the edges would represent routes between the cities.
MST can be applied to optimize airline routes by finding the least costly paths with no
cycles. Minimum spanning tree, can be constructed using any of the following two
algorithms:

1. Kruskal’s algorithm and


2. Prim’s algorithm.

1. Kruskal’s algorithm
Example 1:
Construct the minimal spanning tree for the graph shown below:

Prepared By: Muhammad Imran


Assistant Prof. of Computer Science, Govt. Millat Degree College, Multan.
Lecturer (Visiting Faculty) of I.T, University of Education Multan Campus.
Ch # 7 (Introduction To Graphs) 11

Solution:

Arrange all the edges in the increasing order of their costs:

The stages in Kruskal’s algorithm for minimal spanning tree is as follows:

Prepared By: Muhammad Imran


Assistant Prof. of Computer Science, Govt. Millat Degree College, Multan.
Lecturer (Visiting Faculty) of I.T, University of Education Multan Campus.
Ch # 7 (Introduction To Graphs) 12

Prepared By: Muhammad Imran


Assistant Prof. of Computer Science, Govt. Millat Degree College, Multan.
Lecturer (Visiting Faculty) of I.T, University of Education Multan Campus.

You might also like