Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
4 views

Graph Algorithms (1)

The document provides an overview of graph algorithms, including definitions of graphs, types of edges, and various graph representations such as adjacency matrices and lists. It discusses traversal algorithms like Breadth First Search (BFS) and Depth First Search (DFS), as well as minimum spanning tree algorithms like Prim's and Kruskal's. Additionally, it covers Dijkstra's algorithm for finding the shortest paths in weighted graphs and mentions other algorithms for further study.

Uploaded by

Souvik Majumder
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Graph Algorithms (1)

The document provides an overview of graph algorithms, including definitions of graphs, types of edges, and various graph representations such as adjacency matrices and lists. It discusses traversal algorithms like Breadth First Search (BFS) and Depth First Search (DFS), as well as minimum spanning tree algorithms like Prim's and Kruskal's. Additionally, it covers Dijkstra's algorithm for finding the shortest paths in weighted graphs and mentions other algorithms for further study.

Uploaded by

Souvik Majumder
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 33

Graph Algorithms

Dr. Awnish Kumar


Assistant Professor
Computer Science and Engineering Department
National Institute of Technology Agartala
Introduction
• A graph is a pair (V, E), where V is a set of nodes, called vertices, and E
is a collection of pairs of vertices, called edges.
• Directed edge: ordered pair of vertices (u, v) where first vertex u is the
origin and second vertex v is the destination. Example: one-way road
traffic.

• Undirected edge: unordered pair of vertices (u, v). Example: railway


lines
Introduction
• Directed graph: all the edges are directed.
• Undirected graph: all the edges are undirected.

• When an edge connects two vertices, the vertices are said to be adjacent to each
other and the edge is incident on both vertices.
Introduction
• A graph with no cycles is called a tree. A tree is an acyclic connected
graph.

• A self loop is an edge that connects a vertex to itself.


Introduction
• Two edges are parallel if they connect the same pair of vertices.

• The Degree of a vertex is the number of edges incident on it.


• A subgraph is a subset of a graph’s edges (with associated vertices) that form a
graph.
• A path in a graph is a sequence of adjacent vertices. Simple path is a path with no
repeated vertices. In the graph below, the dotted lines represent a path from G to
F.
Introduction
• A cycle is a path where the first and last vertices are the same. A
simple cycle is a cycle with no repeated vertices or edges (except the
first and last vertices).
Introduction
• We say that one vertex is connected to another if there is a path that
contains both of them.
• A graph is connected if there is a path from every vertex to every
other vertex.
• If a graph is not connected then it consists of a set of connected
components.
Introduction
• A directed acyclic graph [DAG] is a directed graph with no cycles.

• A forest is a disjoint set of trees.


• A spanning tree of a connected graph is a subgraph that contains all of that graph’s vertices and is
a single tree. A spanning forest of a graph is the union of spanning trees of its connected
components.
• A bipartite graph is a graph whose vertices can be divided into two sets such that all edges
connect a vertex in one set with a vertex in the other set.
Introduction
• In weighted graphs integers (weights) are assigned to each edge to
represent (distances or costs).

• Graphs with all edges present are called complete graphs.


Graph Representation
• As in other ADTs, to manipulate graphs we need to represent them in
some useful form. Basically, there are three ways of doing this:
• Adjacency Matrix
• Adjacency List
• Adjacency Set
Adjacency Matrix
• In this method, we use a matrix with size V × V. The values of matrix
are boolean.
• Let us assume the matrix is Adj. The value Adj[u, v] is set to 1 if there
is an edge from vertex u to vertex v and 0 otherwise.
• In the matrix, each edge is represented by two bits for undirected
graphs. That means, an edge from u to v is represented by 1 value in
both Adj[u,v ] and Adj[u,v].
• To save time, we can process only half of this symmetric matrix.
Adjacency Matrix
• If the graph is a directed graph then we need to mark only one entry
in the adjacency matrix. As an example, consider the directed graph
below.
Adjacency Matrix
• To read a graph, one way is to
first read the vertex names
and then read pairs of vertex
names (edges).
• The matrix requires O(V2) bits
of storage and O(V2) time for
initialization.
Adjacency List
• In this representation all the vertices connected to a vertex v are listed on
an adjacency list for that vertex v.
• This can be easily implemented with linked lists. That means, for each
vertex v we use a linked list and list nodes represents the connections
between v and other vertices to which v has an edge.
• The total number of linked lists is equal to the number of vertices in the
graph.
Adjacency List
Adjacency List
• Using adjacency list representation we cannot perform some
operations efficiently. As an example, consider the case of deleting a
node. In adjacency list representation, it is not enough if we simply
delete a node from the list representation. We need to search other
nodes linked list also for deleting it.
Graph Traversals
• To solve problems on graphs, we need a mechanism for traversing the
graphs. Graph traversal algorithms are also called graph search
algorithms.
• Like trees traversal algorithms (Inorder, Preorder, Postorder and Level-
Order traversals), graph search algorithms can be thought of as
starting at some source vertex in a graph and “searching” the graph
by going through the edges and marking the vertices.
• Now, we will discuss two such algorithms for traversing the graphs.
• Breadth First Search [BFS]
• Depth First Search [DFS]
Breadth First Search
• Breadth-first search is one of the simplest algorithms for searching a graph.
• Given a graph G = (V,E) and a distinguished source vertex s, breadth-first search
systematically explores the edges of G to “discover” every vertex that is reachable
from s.
• It computes the distance from s to each reachable vertex, where the distance to a
vertex v equals the smallest number of edges needed to go from s to v.
• Breadth-first search is so named because it expands the frontier between
discovered and undiscovered vertices uniformly across the breadth of the
frontier.
• You can think of it as discovering vertices in waves emanating from the source
vertex. That is, starting from s, the algorithm first discovers all neighbors of s,
which have distance 1. Then it discovers all vertices with distance 2, then all
vertices with distance 3, and so on, until it has discovered every vertex reachable
from s.
Breadth First Search
• To keep track of progress, breadth-first search colors each vertex white, gray, or black. All vertices
start out white, and vertices not reachable from the source vertex s stay white the entire time.
• A vertex that is reachable from s is discovered the first time it is encountered during the search,
at which time it becomes gray, indicating that is now on the frontier of the search.
• The queue contains all the gray vertices. Eventually, all the edges of a gray vertex will be explored,
so that all of its neighbors will be discovered. Once all of a vertex’s edges have been explored, the
vertex is behind the frontier of the search, and it goes from gray to black.
• The breadth-first-search procedure BFS assumes that the graph G = (V, E) is represented using
adjacency lists. It denotes the queue by Q, and it attaches three additional attributes to each
vertex v in the graph:
• v.color is the color of v: WHITE, GRAY, or BLACK.
• v.d holds the distance from the source vertex s to v, as computed by the algorithm.
• v. 𝜋 is v’s predecessor in the breadth-first tree. If v has no predecessor then it is the source vertex
or is undiscovered, then v. 𝜋 = NIL.
Breadth First Search

The total running time of the BFS procedure is


O(V + E). [Cormen]
Breadth First Search
• The PRINT-PATH procedure prints out the vertices on a shortest path
from s to v, assuming that BFS has already computed a breadth-first
tree.
Depth First Search
• It is a recursive algorithm to search all the vertices of a tree data
structure or a graph. The depth-first search (DFS) algorithm starts
with the initial node of graph G and goes deeper until we find the
goal node or the node with no children.

The time complexity of the DFS algorithm is O(V+E),


where V is the number of vertices and E is the number of
edges in the graph.
https://www.javatpoint.com/depth-first-search-algorithm The space complexity of the DFS algorithm is O(V).
Minimum Spanning Tree
• A spanning tree is the subgraph of an undirected connected graph.
• Minimum spanning tree can be defined as the spanning tree in which
the sum of the weights of the edge is minimum.
• The weight of the spanning tree is the sum of the weights given to the
edges of the spanning tree.
• Prim’s Algorithm
• Kruskal’s Algorithm
Kruskal’s Algorithm
• In Kruskal’s algorithm, sort all edges of the given
graph in increasing order.
• Then it keeps on adding new edges and nodes in
the MST if the newly added edge does not form a
cycle.
• It picks the minimum weighted edge at first and the
maximum weighted edge at last.
• Thus we can say that it makes a locally optimal
choice in each step in order to find the optimal
solution.
• The graph contains 9 vertices and 14 edges. So, the
minimum spanning tree formed will be having (9 –
1) = 8 edges.
• https://www.geeksforgeeks.org/kruskals-minimum-
spanning-tree-algorithm-greedy-algo-2/
Prim’s Algorithm
• Step 1: Determine an arbitrary vertex as the
starting vertex of the MST.
• Step 2: Follow steps 3 to 5 till there are vertices
that are not included in the MST (known as
fringe vertex).
• Step 3: Find edges connecting any tree vertex
with the fringe vertices.
• Step 4: Find the minimum among these edges.
• Step 5: Add the chosen edge to the MST if it
does not form any cycle.
• Step 6: Return the MST and exit.
• https://www.geeksforgeeks.org/prims-
minimum-spanning-tree-mst-greedy-algo-5/
Single Source Shortest Path: Djikstra’s Algorithm
• Given a weighted graph and a source vertex in the graph, find the shortest paths from the
source to all the other vertices in the given graph.
• Note: The given graph does not contain any negative edge.
• Create a set sptSet (shortest path tree set) that keeps track of vertices included in the
shortest path tree, i.e., whose minimum distance from the source is calculated and finalized.
Initially, this set is empty.
• Assign a distance value to all vertices in the input graph. Initialize all distance values
as INFINITE. Assign the distance value as 0 for the source vertex so that it is picked first.
• While sptSet doesn’t include all vertices
• Pick a vertex u that is not there in sptSet and has a minimum distance value.
• Include u to sptSet.
• Then update the distance value of all adjacent vertices of u.
• To update the distance values, iterate through all adjacent vertices.
• For every adjacent vertex v, if the sum of the distance value of u (from source) and weight of edge u-v, is less than the
distance value of v, then update the distance value of v.
Djikstra’s Algorithm
• The set sptSet is initially empty and
distances assigned to vertices are {0,
INF, INF, INF, INF, INF, INF, INF}
where INF indicates infinite.
• Now pick the vertex with a minimum
distance value. The vertex 0 is picked,
include it in sptSet.
• So sptSet becomes {0}. After including
0 to sptSet, update distance values of
its adjacent vertices.
• Adjacent vertices of 0 are 1 and 7. The
distance values of 1 and 7 are updated
as 4 and 8.
Djikstra’s Algorithm
• Pick the vertex with minimum distance
value and not already included
in SPT (not in sptSET). The vertex 1 is
picked and added to sptSet.
• So sptSet now becomes {0, 1}. Update
the distance values of adjacent vertices
of 1.
• The distance value of vertex 2
becomes 12.
Djikstra’s Algorithm
• Pick the vertex with minimum distance
value and not already included
in SPT (not in sptSET). Vertex 7 is picked.
So sptSet now becomes {0, 1, 7}.
• Update the distance values of adjacent
vertices of 7. The distance value of
vertex 6 and 8 becomes finite (15 and
9 respectively).
Djikstra’s Algorithm
• Pick the vertex with minimum distance
value and not already included
in SPT (not in sptSET). Vertex 6 is picked.
So sptSet now becomes {0, 1, 7, 6}.
• Update the distance values of adjacent
vertices of 6. The distance value of
vertex 5 and 8 are updated.
Djikstra’s Algorithm
• We repeat the above steps
until sptSet includes all vertices of the
given graph. Finally, we get the
following Shortest Path Tree (SPT).
• https://www.geeksforgeeks.org/dijkstra
s-shortest-path-algorithm-greedy-algo-
7/
Assignment
• Bellman Ford’s Algorithm
• Floyd Warshall algorithm
• Ford Fulkerson method.
Thank You.

You might also like