Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
46 views49 pages

Graph Theory Basics & Applications

Graph theory is the study of graphs, which represent relationships between objects using vertices and edges, and is widely applied in computer science for algorithms such as finding the shortest path. Key concepts include vertices, edges, loops, degrees of vertices, and spanning trees, with algorithms like Prim's and Kruskal's used for minimum spanning trees. The document also discusses shortest path problems and algorithms like Bellman-Ford and Dijkstra's for solving them.

Uploaded by

n0236685b
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views49 pages

Graph Theory Basics & Applications

Graph theory is the study of graphs, which represent relationships between objects using vertices and edges, and is widely applied in computer science for algorithms such as finding the shortest path. Key concepts include vertices, edges, loops, degrees of vertices, and spanning trees, with algorithms like Prim's and Kruskal's used for minimum spanning trees. The document also discusses shortest path problems and algorithms like Bellman-Ford and Dijkstra's for solving them.

Uploaded by

n0236685b
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 49

Graph Theory

Introduction
• In mathematics, graph theory is the study of graphs, which are
mathematical structures used to model pairwise relations between
objects.
• In computer science graph theory is used for the study of algorithms
• A graph in this context is made up of vertices (also called nodes or
points) which are connected by edges (also called links or lines).
• Graph theory is used to find shortest path in road or a network.
• In Google Maps, various locations are represented as vertices or nodes and
the roads are represented as edges and graph theory is used to find the
shortest path between two nodes.
Graph Theory Fundamentals
• A graph is a diagram of points and lines connected to the points. It
has at least one line joining a set of two vertices with no vertex
connecting itself.
• The concept of graphs in graph theory stands up on some basic terms
such as point, line, vertex, edge, degree of vertices, properties of
graphs, etc.
Point
• A point is a particular position in a one-dimensional, two-dimensional,
or three-dimensional space.
• For better understanding, a point can be denoted by an alphabet. It
can be represented with a dot.

Example

• Here, the dot is a point named ‘a’.


Line
• A Line is a connection between two points. It can be represented with
a solid line.

Example

• Here, ‘a’ and ‘b’ are the points. The link between these two points is
called a line.
Vertex
• A vertex is a point where multiple lines meet. It is also called a node.
Similar to points, a vertex is also denoted by an alphabet.

Example

• Here, the vertex is named with an alphabet ‘a’


Edge
• An edge is the mathematical term for a line that connects two vertices.
Many edges can be formed from a single vertex.
• Without a vertex, an edge cannot be formed. There must be a starting
vertex and an ending vertex for an edge.

Example

Here, ‘a’ and ‘b’ are the two vertices and the link between them is called
an edge.
Graph
• A graph ‘G’ is defined as G = (V, E) Where V is a set of all vertices and E is
a set of all edges in the graph.
Example 1 Example 2

• In example 1, ab, ac, cd, and bd are the edges of the graph. Similarly, a,
b, c, and d are the vertices of the graph.
• In example 2, there are four vertices a, b, c, and d, and four edges ab,
ac, ad, and cd.
Loop
• In a graph, if an edge is drawn from vertex to itself, it is called a loop.

• In the above graph, V is a vertex for which it has an edge (V, V)


forming a loop.

• In the above graph, there are two loops which are formed at vertex a,
and vertex b.
Degree of vertex
• It is the number of vertices adjacent to a vertex V.
Notation − deg(V).
• In a simple graph with n number of vertices, the degree of any vertices is −
deg(v) ≤ n – 1 ∀ v ∈ G
• A vertex can form an edge with all other vertices except by itself. So the degree
of a vertex will be up to the number of vertices in the graph minus 1. This 1 is
for the self-vertex as it cannot form a loop by itself. If there is a loop at any of
the vertices, then it is not a Simple Graph.
• Degree of vertex can be considered under two cases of graphs −
• Undirected Graph
• Directed Graph
Degree of Vertex in an Undirected
Graph
• An undirected graph has no directed edges. Consider the following
examples.
• Example 1
• Take a look at the following graph −
In the above Undirected Graph,

• deg(a) = 2, as there are 2 edges meeting at vertex ‘a’.

• deg(b) = 3, as there are 3 edges meeting at vertex ‘b’.

• deg(c) = 1, as there is 1 edge formed at vertex ‘c’

• So ‘c’ is a pendent vertex.

• deg(d) = 2, as there are 2 edges meeting at vertex ‘d’.

• deg(e) = 0, as there are 0 edges formed at vertex ‘e’.

• So ‘e’ is an isolated vertex.


Example 2
• Take a look at the following graph −
• In the above graph,
• deg(a) = 2, deg(b) = 2, deg(c) = 2, deg(d) = 2, and deg(e) = 0.
• The vertex ‘e’ is an isolated vertex. The graph does not have any
pendent vertex.
Degree of Vertex in a Directed
Graph
• In a directed graph, each vertex has an indegree and an outdegree.
• Indegree of a Graph
• Indegree of vertex V is the number of edges which are coming into
the vertex V.
Notation − deg−(V).
• Outdegree of a Graph
• Outdegree of vertex V is the number of edges which are going out
from the vertex V.
Notation − deg+(V).
Example 1
• Take a look at the following directed graph. Vertex ‘a’ has two edges,
‘ad’ and ‘ab’, which are going outwards. Hence its outdegree is 2.
Similarly, there is an edge ‘ga’, coming towards vertex ‘a’. Hence the
indegree of ‘a’ is 1.
Example 1 cont..
• The indegree and outdegree of other vertices are shown in the
following table −
Example 2
• Take a look at the following directed graph. Vertex ‘a’ has an edge ‘ae’
going outwards from vertex ‘a’. Hence its outdegree is 1. Similarly, the
graph has an edge ‘ba’ coming towards vertex ‘a’. Hence the indegree
of ‘a’ is 1.
Example 2 cont..
• The indegree and outdegree of other vertices are shown in the
following table −
Pendent Vertex

• By using degree of a vertex, we have a two special types of vertices. A


vertex with degree one is called a pendent vertex.

• Here, in this example, vertex ‘a’ and vertex ‘b’ have a connected edge
‘ab’. So with respect to the vertex ‘a’, there is only one edge towards
vertex ‘b’ and similarly with respect to the vertex ‘b’, there is only one
edge towards vertex ‘a’. Finally, vertex ‘a’ and vertex ‘b’ has degree as
one which are also called as the pendent vertex.
Isolated Vertex

• A vertex with degree zero is called an isolated vertex.

• Here, the vertex ‘a’ and vertex ‘b’ has a no connectivity between each
other and also to any other vertices. So the degree of both the
vertices ‘a’ and ‘b’ are zero. These are also called as isolated vertices.
Adjacency

Here are the norms of adjacency −


• In a graph, two vertices are said to be adjacent, if there is an edge
between the two vertices. Here, the adjacency of vertices is
maintained by the single edge that is connecting those two vertices.
• In a graph, two edges are said to be adjacent, if there is a common
vertex between the two edges. Here, the adjacency of edges is
maintained by the single vertex that is connecting two edges.
Example 1
In the above graph −
• ‘a’ and ‘b’ are the adjacent vertices, as there is a common edge ‘ab’
between them.
• ‘a’ and ‘d’ are the adjacent vertices, as there is a common edge ‘ad’
between them.
• ab’ and ‘be’ are the adjacent edges, as there is a common vertex ‘b’
between them.
• be’ and ‘de’ are the adjacent edges, as there is a common vertex ‘e’
between them.
Example 2
In the above graph −
• ‘a’ and ‘d’ are the adjacent vertices, as there is a common edge ‘ad’
between them.
• ‘c’ and ‘b’ are the adjacent vertices, as there is a common edge ‘cb’
between them.
• ‘ad’ and ‘cd’ are the adjacent edges, as there is a common vertex ‘d’
between them.
• ‘ac’ and ‘cd’ are the adjacent edges, as there is a common vertex ‘c’
between them.
Parallel Edges

• In a graph, if a pair of vertices is connected by more than one edge,


then those edges are called parallel edges.

• In the above graph, ‘a’ and ‘b’ are the two vertices which are
connected by two edges ‘ab’ and ‘ab’ between them. So it is called as
a parallel edge.
Multi Graph

• A graph having parallel edges is known as a Multigraph.

• In the above graph, there are five edges ‘ab’, ‘ac’, ‘cd’, ‘cd’, and ‘bd’.
Since ‘c’ and ‘d’ have two parallel edges between them, it a
Multigraph.
• In the above graph, the vertices ‘b’ and ‘c’ have two edges. The
vertices ‘e’ and ‘d’ also have two edges between them. Hence it is a
Multigraph.
Degree Sequence of a Graph

• If the degrees of all vertices in a graph are arranged in descending or


ascending order, then the sequence obtained is known as the degree
sequence of the graph.

• In the above graph, for the vertices {d, a, b, c, e}, the degree sequence is
{3, 2, 2, 2, 1}.
• In the above graph, for the vertices {a, b, c, d, e, f}, the degree
sequence is {2, 2, 2, 2, 2, 0}.
Spanning Tree
Introduction
• A spanning tree is defined as a subset of a connected undirected graph that
has all the vertices covered with the minimum number of edges possible. A
graph is a set of vertices and edges that are connected with each other,
there are mainly three types of graphs, which are directed, undirected, and
connected. A directed graph is also known as a digraph, the graph having
edges between vertices along with defined directions is known as a
directed graph. Undirected graphs do not have directions and the
connected graph has each vertex connected with every other vertex
present in the graph.
• The spanning tree covers all the vertex present in the graph. The only
difference between a spanning tree and a graph is that a spanning tree
does not have a cycle and a minimum number of edges possible.
What is a spanning tree
• The spanning tree is a subgraph of an undirected connected graph. It
includes all the vertices in the subgraph and the least number of
edges that can connect every vertex without forming a loop or cycle.

• The spanning tree must have an equal number of vertices as of the


given graph. In the spanning tree, the total number of edges is n-1.
Here, n is the number of vertices in the graph. The edges of the
spanning tree may or may not have weight with them, it depends that
the edges of the actual graph have weight or not.
Properties of spanning trees
• A connected graph can have multiple spanning trees. A graph with n
vertices can have an n^(n-2) number of spanning trees.
• Spanning trees does not have any loop or cycle.
• Spanning trees have n vertices and n-1 number of edges.
• All spanning tree of a graph has equivalent vertices.
• Removing a single edge from the spanning tree will make the graph
disconnected as the spanning tree is minimal connected.
• Adding any edge can create a loop or cycle in the spanning tree.
• Spanning trees can be formed on connected graphs only,
disconnected graphs cannot form spanning trees.
Example of spanning trees
• Consider the following original graph:

• A number of spanning trees are possible from the above graph


Possible spanning trees (rotate)
Minimum Spanning Tree
• A minimum spanning tree or minimum cost spanning tree is that
spanning tree, which covers all the vertices of the graph with
minimum edges and the sum of the weight of those edges is
minimum among other spanning trees of that graph.
• The minimum spanning trees are mainly of two types:
• Prim’s MST
• Kruskal’s MST
Prim’s MST
• Prim’s algorithm starts with a single node/vertex and moves on to the
adjacent vertices to explore all the connected edges. The idea behind
prim’s algorithm is that it maintains two sets of vertices. The first set
comprises vertices that are already included in the minimum spanning
tree and the other set consists of all the vertices which are not
included yet.
Kruskal’s MST
• Kruskal’s algorithm is a greedy algorithm used to find out the shortest path
in a minimum-spanning tree. The algorithm aims to traverse the graph and
detect the subset of edges with minimal value and cover all the vertices of
the graph. At every step of the algorithm and analysis, it follows a greedy
approach for an overall optimized result.
• Kruskal’s algorithm can be summed up as a minimum spanning tree
algorithm taking a graph as input and forms a subset of the edges of the
graph,
• which has a minimum sum of edge weight among all the trees that can be formed
from that graph.
• that form a tree including each vertex of the graph, without forming any cycle
between the vertex.
Applications of Spanning Tree:

• In routing protocols
• Cluster mapping and analysis
• Network Planning
• Explore the path/ route in the maps.
Shortest Path Problem
Introduction
• The shortest path problem is about finding a path between 2 vertices
in a graph such that the total sum of the edges weights is minimum.
• This problem could be solved easily using breadth first search (BFS) if
all edge weights were (1), but here weights can take any value.
• Two different algorithms are discussed below depending on the use-
case.
• Bellman Ford's Algorithm
• Dijkstra's Algorithm
Bellman Ford's Algorithm

• Bellman Ford's algorithm is used to find the shortest paths from the
source vertex to all other vertices in a weighted graph. It depends on
the following concept: Shortest path contains at most �−1 edges,
because the shortest path couldn't have a cycle.
• So why shortest path shouldn't have a cycle ?
There is no need to pass a vertex again, because the shortest path to
all other vertices could be found without the need for a second visit
for any vertices.
Bellman Ford's Algorithm (cont..)

Algorithm Steps:
• The outer loop traverses from 0 : n−1.
• Loop over all edges, check if the next node distance > current node distance +
edge weight, in this case update the next node distance to "current node
distance + edge weight".
This algorithm depends on the relaxation principle where the shortest distance
for all vertices is gradually replaced by more accurate values until eventually
reaching the optimum solution. In the beginning all vertices have a distance of
"Infinity", but only the distance of the source vertex = 0, then update all the
connected vertices with the new distances (source vertex distance + edge
weights), then apply the same concept for the new vertices with new distances
and so on.
Task 1: Analysis
Implement the algorithm using Python then answer the following
questions showing calculations.

• What is the time complexity of this algorithm?


• What is the space complexity of this algorithm?
Dijkstra's Algorithm
Introduction
• Dijkstra's algorithm has many variants but the most common one is to
find the shortest paths from the source vertex to all other vertices in
the graph.
Dijkstra's Algorithm Steps
• Let all vertices distances = infinity except for the source vertex, set the source
distance = 0.
• Push the source vertex in a min-priority queue in the form (distance , vertex), as
the comparison in the min-priority queue will be according to vertices distances.
• Pop the vertex with the minimum distance from the priority queue (at first the
popped vertex = source).
• Update the distances of the connected vertices to the popped vertex in case of
"current vertex distance + edge weight < next vertex distance", then push the
vertex
with the new distance to the priority queue.
• If the popped vertex is visited before, just continue without using it.
• Apply the same algorithm again until the priority queue is empty.
Task 2
Implement the algorithm using Python then answer the following
questions showing calculations.

• What is the time complexity of this algorithm?


• What is the space complexity of this algorithm?
Travelling Salesman
Problem

You might also like