You are on page 1of 31

Graphs

What is a Graph?

Definition − A graph (denoted as G=(V,E) consists of a non-empty set of vertices or nodes V and
a set of edges E.

Example − Let us consider, a Graph is G=(V,E) where V={a,b,c,d} and


E={{a,b},{a,c},{b,c},{c,d}}

Degree of a Vertex − The degree of a vertex V of a graph G (denoted by deg (V)) is the number
of edges incident with the vertex V.

Vertex Degree Even / Odd

a 2 even

b 2 even

c 3 odd

d 1 odd
Even and Odd Vertex − If the degree of a vertex is even, the vertex is called an even vertex and
if the degree of a vertex is odd, the vertex is called an odd vertex.

Degree of a Vertex − The degree of a graph is the largest vertex degree of that graph. For the
above graph the degree of the graph is 3.

The Handshaking Lemma − In a graph, the sum of all the degrees of all the vertices is equal to
twice the number of edges.

Types of Graphs

There are different types of graphs, which we will learn in the following section.

Null Graph

A null graph has no edges. The null graph of nn vertices is denoted by Nn


Simple Graph

A graph is called simple graph/strict graph if the graph is undirected and does not contain any
loops or multiple edges.

Multi-Graph

If in a graph multiple edges between the same set of vertices are allowed, it is called Multigraph.
In other words, it is a graph having at least one loop or multiple edges.

Directed and Undirected Graph

A graph G=(V,E) is called a directed graph if the edge set is made of ordered vertex pair and a
graph is called undirected if the edge set is made of unordered vertex pair.
Connected and Disconnected Graph

A graph is connected if any two vertices of the graph are connected by a path; while a graph is
disconnected if at least two vertices of the graph are not connected by a path. If a graph G is
disconnected, then every maximal connected subgraph of G is called a connected component of
the graph G

.
Regular Graph

A graph is regular if all the vertices of the graph have the same degree. In a regular graph G of
degree r, the degree of each vertex of G is r.

Complete Graph

A graph is called complete graph if every two vertices pair are joined by exactly one edge. The
complete graph with n vertices is denoted by Kn
Cycle Graph

If a graph consists of a single cycle, it is called cycle graph. The cycle graph with n vertices is
denoted by Cn

Bipartite Graph

If the vertex-set of a graph G can be split into two disjoint sets, V1 and V2, in such a way that
each edge in the graph joins a vertex in V1 to a vertex in V2, and there are no edges in G that
connect two vertices in V1 or two vertices in V2, then the graph G is called a bipartite graph.
Complete Bipartite Graph

A complete bipartite graph is a bipartite graph in which each vertex in the first set is joined to
every single vertex in the second set. The complete bipartite graph is denoted by Kx,y where the
graph G contains x vertices in the first set and y vertices in the second set.

Representation of Graphs

There are mainly two ways to represent a graph −

Adjacency Matrix

Adjacency List

Adjacency Matrix

An Adjacency Matrix A[V][V]is a 2D array of size V×V where V is the number of vertices in a
undirected graph. If there is an edge between Vx to Vy then the value of A[Vx][Vy]=
and A[Vy][Vx]=1, otherwise the value will be zero. And for a directed graph, if there is an edge
between Vy to Vx, then the value of A[Vy][Vx]=1 and A[Vy][Vx]=1, otherwise the value will
be zero.
Adjacency Matrix of an Undirected Graph

Let us consider the following undirected graph and construct the adjacency matrix −

Adjacency matrix of the above undirected graph will be −

a b c d

a 0 1 1 0

b 1 0 1 0

c 1 1 0 1

d 0 0 1 0
Adjacency List

In adjacency list, an array (A[V]) of linked lists is used to represent the graph G with V number
of vertices. An entry A[Vx] represents the linked list of vertices adjacent to the Vx−th vertex.
The adjacency list of the undirected graph is as shown in the figure below −

Isomorphism of Graphs

If two graphs G and H contain the same number of vertices connected in the same way, they are
called isomorphic graphs.

It is easier to check non-isomorphism than isomorphism. If any of these following conditions


occurs, then two graphs are non-isomorphic −

The numbers of connected components are different

Vertex-set cardinalities are different

Edge-set cardinalities are different

Degree sequences are different


Example 1:

The following graphs are isomorphic −

Example 2:

Consider the following graphs, are they the isomorphic, i.e. the “same”?

Solution:

No. The left-hand graph has 5 edges; the right hand graph has 6 edges. So they are not
Isomorphic
Example 3: Check if the two graphs are Isomorphic

Number of vertices: both 5.

Number of edges: both 5.

Degrees of corresponding vertices: all degree 2.

Connectedness: Each is fully connected.

Number of connected components: Both 1.

Pairs of connected vertices: All correspond.

Number of loops: 0.

Number of parallel edges: 0.

Everything is equal and so the graphs are isomorphic

Connectivity

A graph is said to be connected if there is a path between every pair of vertex. From every vertex
to any other vertex, there should be some path to traverse. That is called the connectivity of a
graph. A graph with multiple disconnected vertices and edges is said to be disconnected.

Example 1

In the following graph, it is possible to travel from one vertex to any other vertex. For example,
one can traverse from vertex ‘a’ to vertex ‘e’ using the path ‘a-b-e’.
Cut Vertex

Let ‘G’ be a connected graph. A vertex V ∈ G is called a cut vertex of ‘G’, if ‘G-V’ (Delete ‘V’
from ‘G’) results in a disconnected graph. Removing a cut vertex from a graph breaks it in to two
or more graphs.

Note − Removing a cut vertex may render a graph disconnected.

A connected graph ‘G’ may have at most (n–2) cut vertices.

Example

In the following graph, vertices ‘e’ and ‘c’ are the cut vertices.

By removing ‘e’ or ‘c’, the graph will become a disconnected graph


Without ‘g’, there is no path between vertex ‘c’ and vertex ‘h’ and many other. Hence it is a
disconnected graph with cut vertex as ‘e’. Similarly, ‘c’ is also a cut vertex for the above graph.

Cut Edge (Bridge)

Let ‘G’ be a connected graph. An edge ‘e’ ∈ G is called a cut edge if ‘G-e’ results in a
disconnected graph.

If removing an edge in a graph results in to two or more graphs, then that edge is called a Cut
Edge.

Example

In the following graph, the cut edge is [(c, e)]

By removing the edge (c, e) from the graph, it becomes a disconnected graph
In the above graph, removing the edge (c, e) breaks the graph into two which is nothing but a
disconnected graph. Hence, the edge (c, e) is a cut edge of the graph.

Note − Let ‘G’ be a connected graph with ‘n’ vertices, then

a cut edge e ∈ G if and only if the edge ‘e’ is not a part of any cycle in G.

the maximum number of cut edges possible is ‘n-1’.

whenever cut edges exist, cut vertices also exist because at least one vertex of a cut edge is a cut
vertex.

if a cut vertex exists, then a cut edge may or may not exist.

Cut Set of a Graph

Let ‘G’= (V, E) be a connected graph. A subset E’ of E is called a cut set of G if deletion of all
the edges of E’ from G makes G disconnect.

If deleting a certain number of edges from a graph makes it disconnected, then those deleted
edges are called the cut set of the graph.

Example

Take a look at the following graph. Its cut set is E1 = {e1, e3, e5, e8}.
After removing the cut set E1 from the graph, it would appear as follows –

Similarly there are other cut sets that can disconnect the graph −

E3 = {e9} – Smallest cut set of the graph.

E4 = {e3, e4, e5}

Edge Connectivity

Let ‘G’ be a connected graph. The minimum number of edges whose removal makes ‘G’
disconnected is called edge connectivity of G.

Notation − λ(G)

In other words, the number of edges in a smallest cut set of G is called the edge connectivity of
G.

If ‘G’ has a cut edge, then λ(G) is 1. (edge connectivity of G.)

Example

Take a look at the following graph. By removing two minimum edges, the connected graph
becomes disconnected. Hence, its edge connectivity (λ(G)) is 2.
Here are the four ways to disconnect the graph by removing two edges

Vertex Connectivity

Let ‘G’ be a connected graph. The minimum number of vertices whose removal makes ‘G’ either
disconnected or reduces ‘G’ in to a trivial graph is called its vertex connectivity.

Notation − K(G)

Example

In the above graph, removing the vertices ‘e’ and ‘i’ makes the graph disconnected.
If G has a cut vertex, then K(G) = 1.

Notation − For any connected graph G,

K(G) ≤ λ(G) ≤ δ(G)

Vertex connectivity (K(G)), edge connectivity (λ(G)), minimum number of degrees of G(δ(G)).

Example

Calculate λ(G) and K(G) for the following graph −

Solution

From the graph,

δ(G) = 3

K(G) ≤ λ(G) ≤ δ(G) = 3 (1)

K(G) ≥ 2 (2)

Deleting the edges {d, e} and {b, h}, we can disconnect G.

Therefore,

λ(G) = 2

2 ≤ λ(G) ≤ δ(G) = 2 (3)

From (2) and (3), vertex connectivity K(G) = 2


Euler Path and Cycles

Euler Graphs

A connected graph G is called an Euler graph, if there is a closed trail which includes every edge
of the graph G. An Euler path is a path that uses every edge of a graph exactly once. An Euler
path starts and ends at different vertices.

An Euler circuit is a circuit that uses every edge of a graph exactly once. An Euler circuit always
starts and ends at the same vertex. A connected graph G is an Euler graph if and only if all
vertices of G are of even degree, and a connected graph G is Eulerian if and only if its edge set
can be decomposed into cycles.

The above graph is an Euler graph as “a1b2c3d4e5c6f7g” covers all the edges of the graph.
Konigsberg Bridge Problem

Two islands A and B formed by the Pregal river (now Pregolya) in Konigsberg (then the capital
of east Prussia, but now renamed Kaliningrad and in west Soviet Russia) were connected to each
other and to the banks C and D with seven bridges. The problem is to start at any of the four land
areas, A, B, C, or D, walk over each of the seven bridges exactly once and return to the starting
point. Euler modeled the problem representing the four land areas by four vertices, and the seven
bridges by seven edges joining these vertices

We see from the graph G of the Konigsberg bridges that not all its vertices are of even degree.
Thus G is not an Euler graph, and implies that there is no closed walk in G containing all the
edges of G. Hence it is not possible to walk over each of the seven bridges exactly once and
return to the starting point.

Note Two additional bridges have been built since Euler’s day. The first has been built between
land areas C and D and the second between the land areas A and B. Now in the graph of
Konigsberg bridge problem with nine bridges, every vertex is of even degree and the graph is
thus Eulerian. Hence it is now possible to walk over each of the nine bridges exactly once and
return to the starting point

Hamiltonian Graphs

A connected graph G is called Hamiltonian graph if there is a cycle which includes every vertex
of G and the cycle is called Hamiltonian cycle. Hamiltonian walk in graph G is a walk that
passes through each vertex exactly once.

If G is a simple graph with n vertices, where n≥3n≥3 If deg(v)≥ n2 for each vertex v , then the
graph G is Hamiltonian graph. This is called Dirac's Theorem.

If G is a simple graph with n vertices, where n≥2 if deg(x)+deg(y)≥nfor each pair of non-
adjacent vertices x and y, then the graph G is Hamiltonian graph. This is called Ore's theorem.
Shortest Path Algorithm

Dijkstra's algorithm is an algorithm for finding the shortest paths between nodes in a graph,
which may represent, for example, road networks

The algorithm exists in many variants; Dijkstra's original variant found the shortest path between
two nodes, but a more common variant fixes a single node as the "source" node and finds
shortest paths from the source to all other nodes in the graph, producing a shortest-path tree.

For a given source node in the graph, the algorithm finds the shortest path between that node and
every other. It can also be used for finding the shortest paths from a single node to a single
destination node by stopping the algorithm once the shortest path to the destination node has
been determined. For example, if the nodes of the graph represent cities and edge path costs
represent driving distances between pairs of cities connected by a direct road, Dijkstra's
algorithm can be used to find the shortest route between one city and all other cities.

Algorithm:

Let the node at which we are starting be called the initial node. Let the distance of node Y be the
distance from the initial node to Y. Dijkstra's algorithm will assign some initial distance values
and will try to improve them step by step.
1. Assign to every node a tentative distance value: set it to zero for our initial node and to infinity
for all other nodes.

2. Set the initial node as current. Mark all other nodes unvisited. Create a set of all the unvisited
nodes called the unvisited set.

3. For the current node, consider all of its neighbors and calculate their tentative distances.
Compare the newly calculated tentative distance to the current assigned value and assign the
smaller one.

4. When we are done considering all of the neighbors of the current node, mark the current node
as visited and remove it from the unvisited set. A visited node will never be checked again.

5. If the destination node has been marked visited (when planning a route between two specific
nodes) or if the smallest tentative distance among the nodes in the unvisited set is infinity (when
planning a complete traversal; occurs when there is no connection between the initial node and
remaining unvisited nodes), then stop. The algorithm has finished.

6. Otherwise, select the unvisited node that is marked with the smallest tentative distance, set it
as the new "current node", and go back to step 3.

For demonstration we will consider the below graph:

Step Wise Execution

Step 1:
Mark Vertex 1 as the source vertex. Assign a cost zero to Vertex 1 and (infinite to all other
vertices). The state is as follows

Step 2:

For each of the unvisited neighbors (Vertex 2, Vertex 3 and Vertex 4) calculate the minimum
cost as min(current cost of vertex under consideration, sum of cost of vertex 1 and connecting
edge). Mark Vertex 1 as visited , in the diagram we border it black. The new state would be as
follows:

Step 3:

Choose the unvisited vertex with minimum cost (vertex 4) and consider all its unvisited
neighbors (Vertex 5 and Vertex 6) and calculate the minimum cost for both of them. The state is
as follows:
Step 4:

Choose the unvisited vertex with minimum cost (vertex 2 or vertex 5, here we choose vertex 2)
and consider all its unvisited neighbors (Vertex 3 and Vertex 5) and calculate the minimum cost
for both of them. Now, the current cost of Vertex 3 is [4] and the sum of (cost of Vertex 2 + cost
of edge (2,3) ) is 3 + 4 = [7]. Minimum of 4, 7 is 4. Hence the cost of vertex 3 won’t change. By
the same argument the cost of vertex 5 will not change. We just mark the vertex 2 as visited, all
the costs remain same. The state is as follows:

Step 5:

Choose the unvisited vertex with minimum cost (vertex 5) and consider all its unvisited
neighbors (Vertex 3 and Vertex 6) and calculate the minimum cost for both of them. Now, the
current cost of Vertex 3 is [4] and the sum of (cost of Vertex 5 + cost of edge (5,3) ) is 3 + 6 =
[9]. Minimum of 4, 9 is 4. Hence the cost of vertex 3 won’t change. Now, the current cost of
Vertex 6 is [6] and the sum of (cost of Vertex 5 + cost of edge (3,6) ) is 3 + 2 = [5]. Minimum of
6, 5 is 45. Hence the cost of vertex 6 changes to 5. The state is as follows:
Step 6:

Choose the unvisited vertex with minimum cost (vertex 3) and consider all its unvisited
neighbors (none). So mark it visited. The state is as follows:

Step 7:

Choose the unvisited vertex with minimum cost (vertex 6) and consider all its unvisited
neighbors (none). So mark it visited. The state is as follows
Now there is no unvisited vertex left and the execution ends. At the end we know the shortest
paths for all the vertices from the source vertex 1. Even if we know the shortest path length, we
do not know the exact list of vertices which contributes to the shortest path until we maintain
them separately or the data structure supports it.

Planar graph –

A graph G is called a planar graph if it can be drawn in a plane without any edges crossed. If we
draw graph in the plane without edge crossing, it is called embedding the graph in the plane.

Non-planar graph − A graph is non-planar if it cannot be drawn in a plane without graph edges
crossing
A planar graph is a graph that can be embedded in the plane, i.e., it can be drawn on the plane in
such a way that its edges intersect only at their endpoints. In other words, it can be drawn in such
a way that no edges cross each other. Such a drawing is called a plane graph or planar
embedding of the graph. A plane graph can be defined as a planar graph with a mapping from
every node to a point on a plane, and from every edge to a plane curve on that plane, such that
the extreme points of each curve are the points mapped from its end nodes, and all curves are
disjoint except on their extreme points.

Graph Coloring:

Graph coloring is a special case of graph labeling; it is an assignment of labels traditionally


called "colors" to elements of a graph subject to certain constraints. In its simplest form, it is a
way of coloring the vertices of a graph such that no two adjacent vertices share the same color;
this is called a vertex coloring. Similarly, an edge coloring assigns a color to each edge so that no
two adjacent edges share the same color, and a face coloring of a planar graph assigns a color to
each face or region so that no two faces that share a boundary have the same color.

Vertex coloring is the starting point of the subject, and other coloring problems can be
transformed into a vertex version. For example, an edge coloring of a graph is just a vertex
coloring of its line graph, and a face coloring of a plane graph is just a vertex coloring of its dual.
However, non-vertex coloring problems are often stated and studied as is. That is partly for
perspective, and partly because some problems are best studied in non-vertex form, as for
instance is edge coloring.

The convention of using colors originates from coloring the countries of a map, where each face
is literally colored. This was generalized to coloring the faces of a graph embedded in the plane.
By planar duality it became coloring the vertices, and in this form it generalizes to all graphs. In
mathematical and computer representations, it is typical to use the first few positive or
nonnegative integers as the "colors". In general, one can use any finite set as the "color set". The
nature of the coloring problem depends on the number of colors but not on what they are.
Graph coloring enjoys many practical applications as well as theoretical challenges. Beside the
classical types of problems, different limitations can also be set on the graph, or on the way a
color is assigned, or even on the color itself. It has even reached popularity with the general
public in the form of the popular number puzzle Sudoku. Graph coloring is still a very active
field of research.

A proper vertex coloring of the Petersen graph with 3 colors, the minimum number possible.

Travelling Salesman Problem:

The travelling salesman problem (TSP) asks the following question: "Given a list of cities and
the distances between each pair of cities, what is the shortest possible route that visits each city
exactly once and returns to the origin city?"

TSP can be modelled as an undirected weighted graph, such that cities are the graph's vertices,
paths are the graph's edges, and a path's distance is the edge's weight. It is a minimization
problem starting and finishing at a specified vertex after having visited each other vertex exactly
once. Often, the model is a complete graph (i.e. each pair of vertices is connected by an edge). If
no path exists between two cities, adding an arbitrarily long edge will complete the graph
without affecting the optimal tour.
Symmetric TSP with four cities

Web Graph

We can view the static Web consisting of static HTML pages together with the hyperlinks
between them as a directed graph in which each web page is a node and each hyperlink a
directed edge.

Figure shows two nodes A and B from the web graph, each corresponding to a web page, with a
hyperlink from A to B. We refer to the set of all such nodes and directed edges as the web graph.
Figure also shows that (as is the case with most links on web pages) there is some text
surrounding the origin of the hyperlink on page A. This text is generally encapsulated in
the href attribute of the <a> (for anchor) tag that encodes the hyperlink in the HTML code of
page A, and is referred to as anchor text . As one might suspect, this directed graph is
not strongly connected: there are pairs of pages such that one cannot proceed from one page of
the pair to the other by following hyperlinks. We refer to the hyperlinks into a page as in-
links and those out of a page as out-links . The number of in-links to a page (also known as its in-
degree) has averaged from roughly 8 to 15, in a range of studies. We similarly define the out-
degree of a web page to be the number of links out of it. These notions are represented in Figure
A sample small web graph. In this example we have six pages labeled A-F. Page B has in-degree
3 and out-degree 1. This example graph is not strongly connected: there is no path from any of
pages B-F to page A.

There is ample evidence that these links are not randomly distributed; for one thing, the
distribution of the number of links into a web page does not follow the Poisson distribution one
would expect if every web page were to pick the destinations of its links uniformly at random.
Rather, this distribution is widely reported to be a power law , in which the total number of web
pages with in-degree i is proportional to 1/iα; the value of α typically reported by
studies. Furthermore, several studies have suggested that the directed graph connecting web
pages has a bowtie shape: there are three major categories of web pages that are sometimes
referred to as IN, OUT and SCC. A web surfer can pass from any page in IN to any page in SCC,
by following hyperlinks. Likewise, a surfer can pass from page in SCC to any page in OUT.
Finally, the surfer can surf from any page in SCC to any other page in SCC. However, it is not
possible to pass from a page in SCC to any page in IN, or from a page in OUT to a page in SCC
(or, consequently, IN). Notably, in several studies IN and OUT are roughly equal in size,
whereas SCC is somewhat larger; most web pages fall into one of these three sets. The remaining
pages form into tubes that are small sets of pages outside SCC that lead directly from IN to OUT,
and tendrils that either lead nowhere from IN, or from nowhere to OUT.

Google maps:

Google map is an web based application which provides the information about regions and sites,
views streets etc. It also helps in find the shortest path between two places, Google map is using
big gaint graph of world whose vertices are locations (places) and edges as roads/air/rails as
connection between the locations .Weight represent distance or time taken to traverse distance
locations which are connected by edges. With the help of google map one can find the shortest
route from one location to another.Dijkestra’s algorithm is used for this purpose. Since Google
map is a big graph with large number of vertices to reduce the computation time the nodes in the
graph are encompassed with other nodes.These giant nodes encompass the graph of related
nodes.

You might also like