5. Graphs and Graph Algorithms
5. Graphs and Graph Algorithms
1
Graphs
Graph
A data structure that consists of a set of nodes
and a set of edges that relate the nodes to each
other
Vertex
A node in a graph
Edge (arc)
A connection between
two nodes in a graph,
represented by a pair
of vertices.
2
Graphs
Undirected graph
A graph in which the edges have no direction
3
Graphs
4
Graphs
Vertex
Undirected
Graphs
have no
arrows
Edge or arc
on the
edges
5
Graphs
6
Graphs
What
other
structure
is this
?
A tree is a
acyclic
graph.
7
Graphs
Adjacent vertices
Two vertices in a graph that are connected by an edge
Path
A sequence of vertices that connects two nodes in a graph
Complete graph
A graph in which every vertex is directly connected to every
other vertex; For a graph with N nodes,
• N*(N-1) edges in a complete directed graph
• N*(N-1)/2 edges in a complete undirected graph
Weighted graph
A graph in which each edge carries a value
8
Graphs
Multi Graph
If there are numerous edges between a pair of vertices in a graph G= (V, E), the
graph is referred to as a multigraph. There are no self-loops in a Multigraph.
10
Weighted Graph
A graph G= (V, E) is called a labeled or weighted graph because each edge has a
value or weight representing the cost of traversing that edge.
Null Graph
It's a reworked version of a trivial graph. If several vertices but no edges
connect them, a graph G= (V, E) is a null graph.
11
Application of Graphs in Data Structures
Following are some applications of graphs in data structures:
import networkx as nx
import matplotlib.pyplot as plt
# Creating a graph
G = nx.Graph()
# Adding nodes
nodes = ['A', 'B', 'C', 'D', 'E']
G.add_nodes_from(nodes)
# Adding edges
edges = [('A', 'B'), ('A', 'C'), ('B', 'D'), ('C', 'E'), ('D', 'E')]
G.add_edges_from(edges)
Bi-directional Search
14
Un-Informed search
Breadth-First Search (BFS)
• Unlike trees, graphs may contain cycles, so we may come to
the same node again. To avoid processing a node more than
once, we divide the vertices into two categories:
• Visited and
• Not visited.
• A boolean visited array is used to mark the visited vertices.
For simplicity, it is assumed that all vertices are reachable
from the starting vertex. BFS uses a queue data
structure for traversal
Implementation of BFS traversal:
• Follow the below method to implement BFS traversal.
• Declare a queue and insert the starting vertex.
• Initialize a visited array and mark the starting vertex as visited.
• Follow the below process till the queue becomes empty:
• Remove the first vertex of the queue.
• Mark that vertex as visited.
• Insert all the unvisited neighbours of the vertex into the queue.
Un-Informed search
Uniform-Cost Search
Uniform-cost search is a searching algorithm used for
traversing a weighted tree or graph. This algorithm
comes into play when a different cost is available for
each edge. The primary goal of the uniform-cost search
is to find a path to the goal node which has the lowest
cumulative cost. Uniform-cost search expands nodes
according to their path costs form the root node.
Un-Informed search
Depth-First Search
Advantages:
• Bidirectional search is fast.
• Bidirectional search requires less memory
Greedy algorithms
Greedy algorithms are a class of algorithms that make locally optimal choices at each
step with the hope of finding a global optimum solution. In other words, at each step
of the algorithm, the best decision is made based solely on the information available
at that particular step, without considering the consequences of that decision on
future steps. Greedy algorithms are often used for optimization problems where the
goal is to find the best possible solution from a set of choices.
21
Informed search
The A* Search
• A* is an algorithm that:
• Uses heuristic to guide search
• While ensuring that it will compute a path with
minimum cost
“estimated cost”
“actual cost”
A* algorithm
33
Informed search
A* algorithm
A* algorithm
Add g(n) of all nodes along the path, and add h(n) of
only destination node while calculating f(n)
A* algorithm
Start node
End node
29
Simplified steps of Dijkstra algorithm
1. Initialize: Start at the source node and set its distance to 0. Set the distances of all
other nodes to infinity. Keep track of the shortest distance to each node from the
source.
2. Select Node: Choose the unvisited node with the smallest distance from the
source. This node will be the current node.
3. Update Distances: For the current node, consider all of its unvisited neighbors and
calculate their tentative distances through the current node. Compare the newly
calculated tentative distance to the current assigned value and update it if it's
smaller.
4. Mark as Visited: Once all of the neighbors of the current node have been
considered, mark the current node as visited.
5. Repeat: If the destination node has been marked visited or if the smallest tentative
distance among the nodes is infinity (unreachable) then stop. Otherwise, select
the unvisited node that is marked with the smallest tentative distance, set it as the
new "current node," and repeat steps 3-4.
6. Output: The shortest path distances from the source node to all other nodes have
been found. Optionally, backtrack from the destination node using the recorded
shortest path predecessors to find the shortest path itself.
30
Simplified steps of Dijkstra algorithm
7 4
2
2 1
1 1 2 6
Start node 3
4 5
5 End node
3
31
Practice example on Dijkstra algorithm
32
Practice example on Dijkstra algorithm
45
Start node
50 10 End node
1 2 4
35
10 15
20 30
4 15 5 6
3
33
Practice example on Dijkstra algorithm
Start node
End node
34
Practical uses of Dijkstra algorithm
1. GPS Navigation Systems: Dijkstra's algorithm is extensively used in GPS navigation
systems to find the shortest path between two locations. In this context, nodes
represent intersections or waypoints, and edges represent roads or paths between
them, with weights representing distances or travel times. By applying Dijkstra's
algorithm, GPS devices can compute the shortest route from the user's current
location to their desired destination.
2. Network Routing Protocols: Dijkstra's algorithm is employed in various network
routing protocols, such as OSPF (Open Shortest Path First) and IS-IS (Intermediate
System to Intermediate System), to determine the shortest paths in computer
networks. In these protocols, nodes represent routers or network devices, and edges
represent network links with associated costs (e.g., link bandwidth or latency).
Dijkstra's algorithm ensures efficient data routing by finding the shortest paths
between network nodes.
3. Traffic Management Systems: Dijkstra's algorithm is utilized in traffic management
systems to optimize traffic flow and minimize congestion. Nodes in this context
represent intersections or traffic junctions, and edges represent road segments with
associated travel times or congestion levels. By applying Dijkstra's algorithm, traffic
management systems can identify the most efficient routes for vehicles, thereby
reducing travel times and alleviating congestion.
35
Minimum Spanning Tree
40
Practice graph for Prim’s algorithm for
minimum spinning tree
41
Practice graph for Prim’s algorithm for
minimum spinning tree (solution)
42
Kruskal algorithm
Kruskal's algorithm is used to find the minimum spanning tree (MST) of a connected,
undirected graph. Here are the simple steps of Kruskal's algorithm:
1. Sort Edges: Sort all the edges in non-decreasing order of their weights.
2. Initialize MST: Create an empty set to represent the minimum spanning tree (MST).
3. Iterate Through Edges: Iterate through each edge in the sorted order.
4. Check for Cycles: For each edge, check if adding it to the MST would create a cycle.
This can be done by checking if the two vertices of the edge are already in the same
connected component. If adding the edge does not create a cycle, include it in the
MST.
5. Union Operation: If the edge is included in the MST, perform a union operation to
merge the connected co mponents of the two vertices of the edge.
6. Repeat: Repeat steps 3-5 until all vertices are included in the MST or until there are
(V-1) edges in the MST, where V is the number of vertices in the graph.
7. Output MST: The final set of edges forms the minimum spanning tree (MST) of the
graph. 43
Kruskal algorithm steps simplified
The spinning tree should have
44
Example on minimum spinning tree
using Kruskal’s algorithm
45
Practice example on minimum spinning
tree using Kruskal’s algorithm
46