Graph Theory Java Point Notes
Graph Theory Java Point Notes
The interconnected objects are represented by points termed as vertices or nodes and
the links that connect the vertices are called edges or arcs or lines.
1. V = {A, B, C, D, E}
2. E = {AB, BC, CA, AD}
Graph Theory
Graph theory is the sub-field of mathematics and computer science which deals with
graphs, diagrams that contain points and lines and which often pictorially represents
mathematical truths.
In short, graph theory is the study of the relationship between edges and vertices.
Fundamental Concepts
Some of the basic fundamental concepts of graph theory are:
1. Point
A point is a particular position that is located in a space. Space can be one-dimensional,
two-dimensional or three-dimensional space. A dot is used to represent a point in graph
and it is labeled by alphabet, numbers or alphanumeric values.
Example
2. Line
Two points are connected to each other through a line. A line is a connection between
two points. It is represented by a solid line.
Example
Here, 'A' and 'B' are the points and links between two points is called a line.
3. Vertex
A vertex is a synonym of point in graph i.e. one of the points on which the graph is
defined and which may be connected by lines/edges is called a vertex.
Example
4. Edge
Edge is the connection between two vertices. Each edge connects one vertex to another
vertex in the graph. Without a vertex, an edge cannot be formed. It is also called line,
branch, link or arc.
Edge can either be directed or undirected. A directed edge is the edge which points
from one vertex to another, and an undirected edge has no direction.
If there is a directed edge from vertex A to B, and a directed edge from B to A, this
would essentially be equivalent to an undirected edge connecting A and B.
Example
Here, 'A' and 'B' are the vertices and the link 'AB' between them is called an edge.
Graph
Graph specifies to a "function graph" or "graph of a function" i.e. a plot.
A graph G is defined as G = {V, E} where V is a set of all vertices or points and E is the set
of all edges in the graph.
Example 1
In the above example, A, B, C, D and E are the vertices of the graph and AB, BC, CA and
AD are the edges of the graph.
Example 2
Types of Graphs
Though, there are a lot of different types of graphs depending upon the number of
vertices, number of edges, interconnectivity, and their overall structure, some of such
common types of graphs are as follows:
1. Null Graph
A null graph is a graph in which there are no edges between its vertices. A null graph is
also called empty graph.
Example
Example
In the above graph, there is only one vertex 'v' without any edge. Therefore, it is a trivial
graph.
3. Simple Graph
A simple graph is the undirected graph with no parallel edges and no loops.
A simple graph which has n vertices, the degree of every vertex is at most n -1.
Example
In the above example, First graph is not a simple graph because it has two edges
between the vertices A and B and it also has a loop.
Second graph is a simple graph because it does not contain any loop and parallel edges.
4. Undirected Graph
An undirected graph is a graph whose edges are not directed.
Example
In the above graph since there is no directed edges, therefore it is an undirected graph.
5. Directed Graph
A directed graph is a graph in which the edges are directed by arrows.
Example
In the above graph, each edge is directed by the arrow. A directed edge has an arrow
from A to B, means A is related to B, but B is not related to A.
6. Complete Graph
A graph in which every pair of vertices is joined by exactly one edge is called complete
graph. It contains all possible edges.
A complete graph with n vertices contains exactly nC2 edges and is represented by Kn.
Example
In the above example, since each vertex in the graph is connected with all the remaining
vertices through exactly one edge therefore, both graphs are complete graph.
7. Connected Graph
A connected graph is a graph in which we can visit from any one vertex to any other
vertex. In a connected graph, at least one edge or path exists between every pair of
vertices.
Example
In the above example, we can traverse from any one vertex to any other vertex. It means
there exists at least one path between every pair of vertices therefore, it a connected
graph.
8. Disconnected Graph
A disconnected graph is a graph in which any path does not exist between every pair
of vertices.
Example
The above graph consists of two independent components which are disconnected.
Since it is not possible to visit from the vertices of one component to the vertices of
other components therefore, it is a disconnected graph.
9. Regular Graph
A Regular graph is a graph in which degree of all the vertices is same.
Example
In the above example, all the vertices have degree 2. Therefore they are called 2-
Regular graph.
Example 1
In the above example, all the vertices have degree 2. Therefore they all are cyclic graphs.
Example 2
Since, the above graph contains two cycles in it therefore, it is a cyclic graph.
Since, the above graph does not contain any cycle in it therefore, it is an acyclic graph.
A graph G (V, E) is called bipartite graph if its vertex-set V(G) can be decomposed into
two non-empty disjoint subsets V1(G) and V2(G) in such a way that each edge e ∈ E(G)
has its one last joint in V1(G) and other last point in V2(G).
Example 1
Example 2
Example
Example
In the above example, out of n vertices, all the (n-1) vertices are connected to a single
vertex. Hence, it is a star graph.
15 Weighted Graph
A weighted graph is a graph whose edges have been labeled with some weights or
numbers.
The length of a path in a weighted graph is the sum of the weights of all the edges in
the path.
Example
In the above graph, if path is a -> b -> c -> d -> e -> g then the length of the path is 5
+ 4 + 5 + 6 + 5 = 25.
16. Multi-graph
A graph in which there are multiple edges between any pair of vertices or there are
edges from a vertex to itself (loop) is called a multi - graph.
Example
In the above graph, vertex-set B and C are connected with two edges. Similarly, vertex
sets E and F are connected with 3 edges. Therefore, it is a multi graph.
Example
The above graph may not seem to be planar because it has edges crossing each other.
But we can redraw the above graph.
Example
1. Computer Science
In computer science graph theory is used for the study of algorithms like:
o Dijkstra's Algorithm
o Prims's Algorithm
o Kruskal's Algorithm
2. Electrical Engineering
In Electrical Engineering, graph theory is used in designing of circuit connections.
These circuit connections are named as topologies. Some topologies are series, bridge,
star and parallel topologies.
3. Linguistics
o In linguistics, graphs are mostly used for parsing of a language
treeand grammar of a language tree.
o Semantics networks are used within lexical semantics, especially as applied to
computers, modeling word meaning is easier when a given word is understood in
terms of related words.
o Methods in phonology (e.g. theory of optimality, which uses lattice graphs)
and morphology (e.g. morphology of finite - state, using finite-state transducers)
are common in the analysis of language as a graph.
5. Computer Network
o In computer network, the relationships among interconnected
computers within the network, follow the principles of graph theory.
o Graph theory is also used in network security.
o We can use the vertex coloring algorithm to find a proper coloring of the
map with four colors.
o Vertex coloring algorithm may be used for assigning at most four different
frequencies for any GSM (Grouped Special Mobile) mobile phone networks.
6. Social Sciences
o Graph theory is also used in sociology. For example, to explore rumor spreading,
or to measure actors' prestige notably through the use of social network analysis
software.
o Acquaintanceship and friendship graphs describe whether people know each
other or not.
o In influence graphs model, certain people can influence the behavior of others.
o In collaboration graphs model to check whether two people work together in a
particular way, such as acting in a movie together.
7. Biology
o Nodes in biological networks represent bimolecular such as genes, proteins or
metabolites, and edges connecting these nodes indicate functional, physical or
chemical interactions between the corresponding bimolecular.
o Graph theory is used in transcriptional regulation networks.
o It is also used in Metabolic networks.
o In PPI (Protein - Protein interaction) networks graph theory is also useful.
o Characterizing drug - drug target relationships.
8. Mathematics
In mathematics, operational research is the important field. Graph theory provides many
useful applications in operational research. Like:
9. General
Graphs are used to represent the routes between the cities. With the help of tree that is
a type of graph, we can create hierarchical ordered information such as family tree.
Basic Properties of Graph Theory
Properties of graph theory are basically used for characterization of graphs depending
on the structures of the graph. Following are some basic properties of graph theory:
Example
Suppose, we want to find the distance between vertex B and D, then first of all we have
to find the shortest path between vertex B and D.
To count the eccentricity of vertex, we have to find the distance from a vertex to all
other vertices and the highest distance is the eccentricity of that particular vertex.
Example
In the above example, if we want to find the maximum eccentricity of vertex 'a' then:
Hence, the maximum eccentricity of vertex 'a' is 3, which is a maximum distance from
vertex ?a? to all other vertices.
o e(b) = 3
o e(c) = 3
o e(d) = 2
o e(e) = 3
o e(f) = 3
o e(g) = 3
Example
From the example of 5.2, r(G) = 2, which is the minimum eccentricity for the vertex 'd'.
4. Diameter of a Graph
Diameter of a graph is the maximum eccentricity from all the vertices. In other words,
the maximum among all the distances between a vertex to all other vertices is
considered as the diameter of the graph G. It is denoted by d(G).
Example
From the above example, if we see all the eccentricities of the vertices in a graph, we will
see that the diameter of the graph is the maximum of all those eccentricities.
5. Central point
If the eccentricity of the graph is equal to its radius, then it is known as central point of
the graph.
Or
Example
From the above example, 'd' is the central point of the graph. i.e.
1. e(d) = r(d) = 2
6. Centre
The set of all the central point of the graph is known as centre of the graph.
Example
From the example of 5.2, {'d'} is the centre of the graph.
7. Circumference
The total number of edges in the longest cycle of graph G is known as the
circumference of G.
Example
In the above example, the circumference is 6, which is derived from the longest path a
-> c -> f -> g -> e -> b -> a or a -> c -> f -> d -> e -> b -> a.
8. Girth
The total number of edges in the shortest cycle of graph G is known as girth. It is
denoted by g(G).
Example
In the above example, the girth of the graph is 4, which is derived from the shortest
cycle a -> c -> f -> d -> a, d -> f -> g -> e -> d or a -> b -> e -> d -> a.
In other words, for any graph, the sum of degrees of vertices equals twice the number of
edges.
Corollary 1
For directed graph G = (V, E) where, Vertex Set V = {V1, V2, ... Vn} then,
Corollary 2
The number of vertices in any non- directed graph with odd degree is even.
Example
It is impossible to make a graph with v (number of vertices) = 6 where the vertices have
degrees 1, 2, 2, 3, 3, 4. This is because the sum of the degrees deg(V) is,
1. deg(V) = 1 + 2 + 2 + 3 + 3 + 4 = 15</p>
2. deg(V) is always an even number but 15 is odd!
Corollary 3
1. k|V| = 2|E|
Corollary 4
1. k|V| ≤ 2|E|
Corollary 5
1. k|V| ≥ 2|E|
Graph Representations
In graph theory, a graph representation is a technique to store graph into the memory
of computer.
To represent a graph, we just need the set of vertices, and for each vertex the neighbors
of the vertex (vertices which is directly connected to it by an edge). If it is a weighted
graph, then the weight will be associated with each edge.
There are different ways to optimally represent a graph, depending on the density of its
edges, type of operations to be performed and ease of use.
1. Adjacency Matrix
Note, even if the graph on 100 vertices contains only 1 edge, we still have to have a
100x100 matrix with lots of zeroes.
o If there is any weighted graph then instead of 1s and 0s, we can store the weight of the
edge.
Example
Consider the following undirected graph representation:
In the above examples, 1 represents an edge from row vertex to column vertex, and 0
represents no edge from row vertex to column vertex.
Cons: It takes a lot of space and time to visit all the neighbors of a vertex, we have to
traverse all the vertices in the graph, which takes quite some time.
2. Incidence Matrix
In Incidence matrix representation, graph can be represented using a matrix of size:
It means if a graph has 4 vertices and 6 edges, then it can be represented using a matrix
of 4X6 class. In this matrix, columns represent edges and rows represent vertices.
Example
Consider the following directed graph representation.
3. Adjacency List
Example
Let's see the following directed graph representation implemented using linked list:
Cons:
o The adjacency list allows testing whether two vertices are adjacent to each other but it is
slower to support this operation.
Tree and Forest
1. What is Tree and Forest?
Tree
o In graph theory, a tree is an undirected, connected and acyclic graph. In other words,
a connected graph that does not contain even a single cycle is called a tree.
o A tree represents hierarchical structure in a graphical form.
o The elements of trees are called their nodes and the edges of the tree are called
branches.
o A tree with n vertices has (n-1) edges.
o Trees provide many useful applications as simple as a family tree to as complex as trees
in data structures of computer science.
o A leaf in a tree is a vertex of degree 1 or any vertex having no children is called a leaf.
Example
In the above example, all are trees with fewer than 6 vertices.
Forest
In graph theory, a forest is an undirected, disconnected, acyclic graph. In other
words, a disjoint collection of trees is known as forest. Each component of a forest is
tree.
Example
The above graph looks like a two sub-graphs but it is a single disconnected graph.
There are no cycles in the above graph. Therefore it is a forest.
2. Properties of Trees
1. Every tree which has at least two vertices should have at least two leaves.
2. Trees have many characterizations:
Let T be a graph with n vertices, then the following statements are equivalent:
o T is a tree.
o T contains no cycles and has n-1 edges.
o T is connected and has (n -1) edge.
o T is connected graph, and every edge is a cut-edge.
o Any two vertices of graph T are connected by exactly one path.
o T contains no cycles, and for any new edge e, the graph T+ e has exactly one
cycle.
3. Every edge of a tree is cut -edge.
4. Adding one edge to a tree defines exactly one cycle.
5. Every connected graph contains a spanning tree.
6. Every tree has at least two vertices of degree two.
3. Spanning Tree
A spanning tree in a connected graph G is a sub-graph H of G that includes all the
vertices of G and is also a tree.
Example
Consider the following graph G:
From the above graph G we can implement following three spanning trees H:
Example
o Consider a graph G,
o If we remove the edge ac which destroy the cycle a-d-c-a in the above graph then we
get the following graph:
o Remove the edge cb, which destroy the cycle a-d-c-b-a from the above graph then we
get the following graph:
o If we remove the edge ec, which destroy the cycle d-e-c-d from the above graph then we
get the following spanning tree:
2. Building - up method
o Select edges of graph G one at a time. In such a way that there are no cycles are created.
o Repeat this process until all the vertices are included.
Example
Consider the following graph G,
o Next, choose the edge cb, then finally we get the following spanning tree:
Circuit Rank
The number of edges we need to delete from G in order to get a spanning tree.
Example
1. G = m - (n - 1)
2. = 7 - (5 - 1)
3. =3
Connectivity
o Connectivity is a basic concept of graph theory. It defines whether a graph is connected
or disconnected. Without connectivity, it is not possible to traverse a graph from one
vertex to another vertex.
o A graph is said to be connected graph if there is a path between every pair of vertex.
From every vertex to any other vertex there must be some path to traverse. This is called
the connectivity of a graph.
o A graph is said to be disconnected, if there exists multiple disconnected vertices and
edges.
o Graph connectivity theories are essential in network applications, routing transportation
networks, network tolerance etc.
Example
In the above example, it is possible to travel from one vertex to another vertex. Here, we
can traverse from vertex B to H using the path B -> A -> D -> F -> E -> H. Hence it is a
connected graph.
Example
In the above example, it is not possible to traverse from vertex B to H because there is
no path between them directly or indirectly. Hence, it is a disconnected graph.
Let's see some basic concepts of Connectivity.
1. Cut Vertex
A single vertex whose removal disconnects a graph is called a cut-vertex.
When we remove a vertex from a graph then graph will break into two or more graphs.
This vertex is called a cut vertex.
Example 1
Example 2
In the above graph, vertex 'e' is a cut-vertex. After removing vertex 'e' from the above
graph the graph will become a disconnected graph.
When we remove an edge from a graph then graph will break into two or more graphs.
This removal edge is called a cut edge or bridge.
Example 1
In the above graph, edge (c, e) is a cut-edge. After removing this edge from the above
graph the graph will become a disconnected graph.
Example 2
In the above graph, edge (c, e) is a cut-edge. After removing this edge from the above
graph the graph will become a disconnected graph.
3. Cut Set
In a connected graph G, a cut set is a set S of edges with the following properties:
Example 1
To disconnect the above graph G, we have to remove the three edges. i.e. bd, be and ce.
We cannot disconnect it by removing just two of three edges. Hence, {bd, be, ce} is a cut
set.
After removing the cut set from the above graph, it would look like as follows:
4. Edge Connectivity
The edge connectivity of a connected graph G is the minimum number of edges whose
removal makes G disconnected. It is denoted by λ(G).
Example
Let's see an example,
From the above graph, by removing two minimum edges, the connected graph
becomes disconnected graph. Hence, its edge connectivity is 2. Therefore the above
graph is a 2-edge-connected graph.
Here are the following four ways to disconnect the graph by removing two edges:
5. Vertex Connectivity
The connectivity (or vertex connectivity) of a connected graph G is the minimum
number of vertices whose removal makes G disconnects or reduces to a trivial graph. It
is denoted by K(G).
Example
Let's see an example:
The above graph G can be disconnected by removal of the single vertex either 'c' or 'd'.
Hence, its vertex connectivity is 1. Therefore, it is a 1-connected graph.
Coverings
A graph covering of a graph G is a sub-graph of G which contains either all the vertices
or all the edges corresponding to some other graph.
A sub-graph which contains all the vertices is called a line/edge covering. A sub-graph
which contains all the edges is called a vertex covering.
1. Edge Covering
A set of edges which covers all the vertices of a graph G, is called a line cover or edge
cover of G.
Edge covering does not exist if and only if G has an isolated vertex.
Example
In the above graph, the red edges represent the edges in the edge cover of the graph.
Or minimal edge cover is an edge cover of graph G that is not a proper subset of any
other edge cover.
From the above graph, the sub-graph having edge covering are:
Here, M1, M2, M3 are minimal line coverings, but M4 is not because we can delete {b, c}.
Every minimum edge cover is a minimal edge cove, but the converse does not
necessarily exist.
The number of edges in a minimum line covering in G is called the line covering number
of G and it is denoted by α1.
Example
From the above graph, the sub-graph having edge covering are:
M1 = {{a, b}, {c, d}}
M2 = {{a, d}, {b, c}}
M3 = {{a, b}, {b, c}, {b, d}}
M4 = {{a, b}, {b, c}, {c, d}}
In the above example, M1 and M2 are the minimum edge covering of G and α1= 2.
2. Vertex Covering
A set of vertices which covers all the nodes/vertices of a graph G, is called a vertex
cover for G.
Example
In the above example, each red marked vertex is the vertex cover of graph. Here, the set
of all red vertices in each graph touches every edge in the graph.
Example
The sub- graphs that can be derived from the above graph are:
M1 = {b, c}
M2 = {a, b, c}
M3 = {b, c, d}
Here, M1 and M2 are minimal vertex coverings, but in M3 vertex 'd' can be deleted.
The number of vertices in a minimum vertex covering in a graph G is called the vertex
covering number of G and it is denoted by α2.
Example 1
In the above graphs, the vertices in the minimum vertex covered are red.
Example 2
The sub- graphs that can be derived from the above graph are:
M1 = {b, c}
M2 = {a, b, c}
M3 = {b, c, d}