Professional Documents
Culture Documents
CH # 7 (Graph)
CH # 7 (Graph)
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.
Example
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
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)).
Types of Graphs
1. Undirected Graph
2. Directed Graph
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.
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.
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
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.
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.
3. Adjacency List
In this representation, every vertex of graph contains list of its adjacent vertices.
For example, consider the following directed graph representation implemented using
linked list...
• 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
Example 1:
Construct the minimal spanning tree for the graph shown below:
Solution: