2. LEARNING OBJECTIVES
After completing this chapter, you will be able to:
1. Represent the graph using array and Linked list
2. Traverse the graph
3. Calculate minimum cost spanning tree
4. Calculate the shortest route from source to all
other nodes
3. GRAPHS
Graph is a collection of nodes or vertices connected
together through edges or arcs.
It is representation of a set of objects where some
pairs of objects are connected by links. The
interconnected objects are represented by points
termed as vertices, and the links that connect the
vertices are called edges.
Formally, a graph is a pair of sets V, E, where V is the
set of vertices and E is the set of edges, connecting
the pairs of vertices.
Take a look at the following graph −
5. USES OF GRAPHS
Graphs are used to model electrical circuits,
chemical compounds, highway maps, and so on.
They are also used in the analysis of electrical
circuits, finding the shortest route, project
planning, linguistics, genetics, social science, and
so forth.
6. GRAPH DEFINITIONS AND NOTATIONS
A graph G is a pair, G = (V, E),
where V is a finite nonempty set, called the set of vertices of
G. E is called the set of edges.
Let V(G) denote the set of vertices, and E(G) denote the set
of edges of a graph G.
If the elements of E(G) are ordered pairs, G is called a
directed graph or digraph;,
otherwise, G is called an undirected graph. In an
undirected graph, the pairs (u, v) and (v, u) represent the
same edge.
7. CONT.…
Let G be a graph.
A graph H is called a sub-graph of G if V(H) ⊆
V(G) and E(H) ⊆ ^E(G);
that is, every vertex of H is a vertex of G, and
every edge in H is an edge in G.
8. CONT.….
A graph can be shown pictorially.
The vertices are drawn as circles, and a label
inside the circle represents the vertex.
1. In an undirected graph, the edges are drawn using
lines.
2. In a directed graph, the edges are drawn using
arrows.
10. GRAPH DATA STRUCTURE
Mathematical graphs can be represented in data-
structure.
We can represent a graph using an array of
vertices and a two dimensional array of edges
11. IMPORTANT GRAPH’S TERMS
Vertex − Each node of the graph is represented as a
vertex. In example given below, labeled circle represents
vertices.
So A to E are vertices. We can represent them using an array
as shown in image below. Here A can be identified by index
0. B can be identified using index 1 and so on.
Edge − Edge represents a path between two vertices or a
line between two vertices.
In example given below, lines from A to B, B to D and
so on represents edges..
12. CONT.…..
Adjacency − Two node or vertices are adjacent if they
are connected to each other through an edge. In
example given below, B is adjacent to A, D is adjacent to
B and so on.
Path − Path represents a sequence of edges between
two vertices. In example given below, ABDE represents a
path from A to E.
13. GRAPH REPRESENTATION
A graph can be represented in several
ways.
Two common ways:
1. adjacency matrices and
2. adjacency lists.
14. ADJACENCY MATRIX
Let G be a graph with n vertices, where n > 0.
Let V(G) = {v1, v2, ..., vn}.
The adjacency matrix AG is a two dimensional matrix n x
^n matrix such that the (i, j)th entry of AG is 1 if there is
an edge from vi to vj;
otherwise, the (i,j)th entry is zero.
16. ADJACENCY LISTS
An Adjacency List is a linked list of Vertices adjacent to a given
vertex
Let G be a graph with n vertices, where n > 0.
Let V(G) = {v1, v2, ..., vn}.
In the adjacency list representation, corresponding to each vertex,
v, there is a linked list such that each node of the linked list
contains the vertex u, such that (v, u) ⊆ E(G).
Because there are n nodes, we use an array, A, of size n, such that
A[i] is a reference variable pointing to the first node of the linked
list containing the vertices to which vi is adjacent. Each node has
two components, say vertex and link.
The component vertex contains the index of the vertex adjacent to
vertex i
18. OPERATIONS ON GRAPHS
Following are the basic primary operations of a Graph
1. Add Vertex − add a vertex to a graph.
2. Add Edge − add an edge between two vertices of a graph.
3. Display Vertex − display a vertex of a graph.
4. Create the graph. That is, store the graph in computer
memory using a particular graph representation.
5. Clear the graph. This operation makes the graph empty.
6. Determine whether the graph is empty.
7. Traverse the graph.
19. GRAPH TRAVERSALS
Processing a graph requires the ability to traverse the
graph. Traversing a graph is similar to traversing a binary
tree, except that traversing a graph is a bit more
complicated. Recall that a binary tree has no cycles.
Also, starting at the root node, we can traverse the entire
tree.
On the other hand, a graph might have cycles and we
might not be able to traverse the entire graph from a single
vertex (for example, if the graph is not connected).
Therefore, we must keep track of the vertices that have
been visited. We must also traverse the graph from each
vertex (that has not been visited) of the graph.
20. CONT.….
This ensures that the entire graph is traversed.
The two most common graph traversal
algorithms are:
1. Depth first traversal
2. Breadth first traversal
21. DEPTH FIRST TRAVERSAL
Depth First Search algorithm DFS traverses a graph in a depth
ward motion and uses a stack to remember to get the next
vertex to start a search when a dead end occurs in any
iteration.
Depth First Search- a search that begins by visiting vertex V,
an then recursively searches the unvisited vertices adjacent to V.
The depth first traversal is similar to the preorder traversal of a
binary tree.
An initial or source vertex is identified to start traversing, then
from that vertex any one vertex which is adjacent to the current
vertex is traversed i.e. only one adjacent vertex is traversed
from the vertex which had been traversed last.
23. DEPTH FIRST TRAVERSAL
As in example given above, DFS algorithm traverses from A to B
to C to D first then to E, then to F and lastly to G.
24. CONT…
It employs following rules.
Rule 1 − Visit adjacent unvisited vertex. Mark it visited.
Display it. Push it in a stack.
Rule 2 − If no adjacent vertex found, pop up a vertex
from stack.
Rule 3 − Repeat Rule 1 and Rule 2 until stack is empty.
31. CONT.…..
As C does not have any unvisited adjacent node so we keep popping the
stack until we find a node which has unvisited adjacent node. In this
case, there's none and we keep popping until stack is empty.
32. BREADTH FIRST TRAVERSAL
Breadth First Search algorithm BFS traverses a graph in a
breadth wards motion and uses a queue to remember to
get the next vertex to start a search when a dead end
occurs in any iteration.
Breadth-First search- A search that begins by visiting
vertex V, then visits the vertices adjacent to V, then visit the
vertices adjacent to each of V’s adjacent vertices, and so
on.
The breadth first traversal of a graph is similar to traversing
a binary tree level by level (the nodes at each level are
visited from left to right).All the nodes at any level, i, are
visited before visiting the nodes at level i + 1.
34. CONT.…
As in example given above, BFS algorithm traverses from A to B to E
to F first then to C and G lastly to D.
35. CONT…
It employs following rules.
Rule 1 − Visit adjacent unvisited vertex. Mark it visited.
Display it. Insert it in a queue.
Rule 2 − If no adjacent vertex found, remove the first
vertex from queue.
Rule 3 − Repeat Rule 1 and Rule 2 until queue is empty.
42. CONT.…..
At this stage we are left with no unmarked unvisited nodes. But as per
algorithm we keep on dequeuing in order to get all unvisited nodes.
When the queue gets emptied the program is over.
43. SHORTEST PATH ALGORITHM
Shortest path can be calculated only for the weighted
graphs.
The edges connecting two vertices can be assigned a
nonnegative real number, called the weight of the edge.
A graph with such weighted edges is called a weighted
graph.
Let G be a weighted graph. Let u and v be two vertices in
G, and let P be a path in G from u to v.
The weight of the path P is the sum of the weights of all
the edges on the path P, which is also called the weight of
v from u via P.
44. CONT.….
Let G be a weighted graph representing a highway
structure. Suppose that the weight of an edge represents
the travel time.
For example, to plan monthly business trips, a
salesperson wants to find the shortest path (that is, the
path with the smallest weight) from her or his city to
every other city in the graph. Many such problems exist in
which we want to find the shortest path from a given
vertex, called the source, to every other vertex in the
graph.
This section describes the shortest path algorithm, also
called the greedy algorithm, developed by Dijkstra.
45. SHORTEST PATH
Given a vertex, say vertex (that is, a source), this section
describes the shortest path algorithm.
The general algorithm is:
1. Initialize the array smallest Weight so that smallest
Weight[u]=weights[vertex, u].
2. Set smallest Weight[vertex] = 0.
3. Find the vertex, v, that is closest to vertex for which the shortest path has
not been determined.
4. Mark v as the (next) vertex for which the smallest weight is found.
5. For each vertex w in G, such that the shortest path from vertex to w has
not been determined and an edge (v, w) exists, if the weight of the path
to w via v is smaller than its current weight, update the weight of w to the
weight of v + the weight of the edge (v, w).
49. MINIMUM SPANNING TREES
A spanning tree of a graph, G, is a set of |V|-1 edges that connect all vertices
of the graph.
Suppose we have a group of islands that we wish to link with bridges so that
it is possible to travel from one island to any other in the group.
Further suppose that (as usual) our government wishes to spend the
absolute minimum amount on this project (because other factors like the
cost of using, maintaining, etc., these bridges will probably be the
responsibility of some future government). The engineers are able to
produce a cost for a bridge linking each possible pair of islands. The set of
bridges which will enable one to travel from any island to any other at
minimum capital cost to the government is the minimum spanning tree.
In general, it is possible to construct multiple spanning trees for a graph, G.
If a cost, cij, is associated with each edge, eij = (vi,vj), then the minimum
spanning tree is the set of edges, E span, forming a spanning tree, such that:
C = sum( cij | all eij in Espan ) is a minimum.
50. KRUSKAL'S ALGORITHM
This algorithm creates a forest of trees. Initially the
forest consists of n single node trees (and no edges). At
each step, we add one (the cheapest one) edge so that
it joins two trees together.
If it were to form a cycle, it would simply link two
nodes that were already part of a single connected tree,
so that this edge would not be needed.
57. SUMMARY
Graph is a collection of nodes Connected together using
edges.
Graph can be traversed using DFS or BFS
Shortest path for a vertex with other vertices can be
calculated using Dijkstra’s algorithm.
Spanning tree is an acyclic graph.
Minimum cost spanning trees can be derived using
Kruskal’s algorithm