0% found this document useful (0 votes)
14 views

Data Structure 25 + 26

The document covers graph algorithms, including topological sort, greedy algorithms, and traversal methods such as breadth-first and depth-first search. It explains the structure of graphs, types of graphs (undirected, directed, weighted), and key concepts like cycles and degrees of nodes. Additionally, it details specific algorithms like Dijkstra's and Kruskal's for finding the shortest path and minimum spanning tree, respectively.

Uploaded by

Faisal Sheikh
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)
14 views

Data Structure 25 + 26

The document covers graph algorithms, including topological sort, greedy algorithms, and traversal methods such as breadth-first and depth-first search. It explains the structure of graphs, types of graphs (undirected, directed, weighted), and key concepts like cycles and degrees of nodes. Additionally, it details specific algorithms like Dijkstra's and Kruskal's for finding the shortest path and minimum spanning tree, respectively.

Uploaded by

Faisal Sheikh
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/ 27

DATA

STRUCTURE
Lecture 25 + 26
Instructor: Tayyba khalid
Content:

 Graph algorithms
 Topological sort
 Greedy Algorithms
 Breadth-first and Depth-first traversal
 Shortest path
Graphs

■ Graphs are non linear data structure. A graph is made up of sets of


nodes and lines. Nodes are called vertices or points.
■ Lines are called edges or arcs. Lines are used to connect odes.
■ An edge of graph represented as e = [ u , v ]
■ U and v denoted as start and end node of edge.
■ Undirected Graph:
5
An undirected graph has edges that have no direction.
0 7

■ Directed Graph: 5

A directed graph has edges that are unidirectional.


4 2

■ Weighted Graph. 6 8

A graph which has a weight, or number, associated with each edge is


called weighted graph.

1
250 53
0
3 2
30
■ Degree of node:
The number of edges that a node contain is called degree of node.
■ Out degree
Number of edges outgoing from node.
■ In degree
Number of edges toward node.
■ Cycle
A path which originates or begins and ends at the same node is called
Cycle.
■ A cycle
A directed graph that has no cycle is called acyclic graph. A cyclic graph
is also referred to as DGA.
Graph Algorithms:
■ Graph algorithms are methods used to manipulate and analyze graphs,
solving various range of problems like finding the shortest path, cycles
detection.
■ Graph Representations
– adjacency matrix
– adjacency list
– edge list
■ Graph Traversals:
•Breadth-First Search (BFS)
•Depth-First Search (DFS)
■ Shortest Path Algorithms:
Topological Sorting

■ Topological sorting for Directed Acyclic Graph (DAG) is a linear


ordering of vertices such that for every directed edge u-v,
vertex u comes before v in the ordering.
■ Note: Topological Sorting for a graph is not possible if the graph is not
a DAG.

■ Working in Class.
Applications of Topological Sort:

■ Task Scheduling: Topological sorting is often used in scenarios where


tasks must be completed in a particular order, such as scheduling
tasks in project management or building software (dependency
resolution).
■ Compilation Order: Compilers can use topological sorting to determine
in which order to compile files based on their dependencies
Example:

Output will be:

452310
Greedy Algorithm
■ A greedy algorithm solves problems by making the best choice at each step.
Instead of looking at all possible solutions, it focuses on the option that seems
best right now.

■ Problem structure:

■ Most of the problems where greedy algorithms work follow these two
properties:
■ 1). Greedy Choice Property:- This property states that choosing the best
possible option at each step will lead to the best overall solution. If this is not
true, a greedy approach may not work.
■ 2). Optimal Substructure:- This means that you can break the problem down
into smaller parts, and solving these smaller parts by making greedy choices
helps solve the overall problem.
■ Some popular Greedy Algorithms are Fractional Knapsack, Dijkstra’s algorithm,
Kruskal’s algorithm, Huffman coding and Prim’s Algorithm
Kruskal’s Algorithm

■ Kruskal’s algorithm to find the MST of a given weighted graph.

■ 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. Hence this is a Greedy Algorithm.
How to find MST using Kruskal’s
algorithm?

1. Sort all edges of the graph in non-decreasing order of their weights.


2. Initialize an empty spanning tree.
3. Pick edges one by one, starting from the smallest:
4. If adding an edge to the spanning tree does not form a cycle, add it to the
tree.
5. If adding the edge forms a cycle, discard it.
6. Continue this process until the spanning tree contains V−1 edges, where V is
the number of vertices in the graph.
Example:

The graph contains 9 vertices and 14 edges.


So, the minimum spanning tree formed will be having (9 – 1) = 8 edges.
Weight Source Destination
1 7 6
2 8 2
2 6 5
4 0 1
4 2 5
6 8 6
7 2 3
7 7 8
8 0 7
8 1 2
Now pick all edges
one by one from the 9 3 4
sorted list of edges 10 5 4
11 1 7
14 3 5
Total number of edges = 8

Total weight= 4 + 8 + 1 + 2
+4+2+7+9 = 3
Breadth first search

■ Breadth First Search (BFS) is a fundamental graph traversal


algorithm. It begins with a node, then first traverses all its adjacent
nodes. Once all adjacent are visited, then their adjacent are traversed.
• BFS is different from DFS in a way that closest vertices are visited before
others. We mainly traverse vertices level by level.

■ BFS itself can be used to detect cycle in a directed and undirected graph,
find shortest path in an unweighted graph and many more problems.

■ Implementation in Class:
Applications of BFS in
Graphs:
■ BFS has various applications in graph theory and computer science,
including:
• Shortest Path Finding: BFS can be used to find the shortest path
between two nodes in an unweighted graph. By keeping track of the parent
of each node during the traversal, the shortest path can be reconstructed.

• Cycle Detection: BFS can be used to detect cycles in a graph. If a node is


visited twice during the traversal, it indicates the presence of a cycle.

• Connected Components: BFS can be used to identify connected


components in a graph. Each connected component is a set of nodes that
can be reached from each other.

• Topological Sorting: BFS can be used to perform topological sorting on a


directed acyclic graph (DAG). Topological sorting arranges the nodes in a
linear order such that for any edge (u, v), u appears before v in the order.
Depth First Search

■ In Depth First Search (or DFS) for a graph, we traverse all adjacent
vertices one by one. When we traverse an adjacent vertex, we completely
finish the traversal of all vertices reachable through that adjacent vertex.
■ This is similar to a tree, where we first completely traverse the left subtree
and then move to the right subtree.
■ The key difference is that, unlike trees, graphs may contain cycles (a node
may be visited more than once).
■ To avoid processing a node multiple times, we use a boolean visited array.

■ Implementation in Class:
Difference between BFS/DFS

■ Breadth-First Search (BFS) and Depth-First Search (DFS) are two


fundamental algorithms used for traversing or searching graphs and
trees.
Parameters BFS DFS

BFS stands for Breadth First DFS stands for Depth First
Stands for Search. Search.

BFS(Breadth First Search) uses


DFS(Depth First Search) uses
Data Structure Queue data structure for
Stack data structure.
finding the shortest path.

BFS builds the tree level by DFS builds the tree sub-tree by
Conceptual Difference level. sub-tree.

It works on the concept of FIFO It works on the concept of LIFO


Approach used (First In First Out). (Last In First Out).

BFS is used in various


applications such as shortest
There are many applications
paths, etc.
where both BFS and DFS can be
Applications If weight of every edge is
used like Topological Sorting,
same, then BFS gives shortest
Cycle Detection, etc.
pat from source to every other
vertex.
Shortest Path Algorithm
Dijkstra’s Algorithm:

■ It starts at the source vertex and iteratively selects the unvisited


vertex with the smallest tentative distance from the source.
■ It then visits the neighbors of this vertex and updates their tentative
distances if a shorter path is found.
■ This process continues until the destination vertex is reached, or all
reachable vertices have been visited.
Here's how it works step-by-step:

1. Initialization:
Set of nodes: Start with a graph where each node has a distance.
Unvisited nodes: All nodes are unvisited at the beginning. The algorithm
proceeds by visiting one node at a time.
Previous nodes: Keep track of the shortest path tree by storing the
previous node for each visited node.
2. Set the start node:
The algorithm starts from the source node, and it looks at all neighboring
nodes and calculates their tentative distances.
3. Visit the nearest unvisited node:
Among all unvisited nodes, pick the one with the smallest tentative
distance and visit it.
Mark this node as "visited." A visited node will not be checked again.
4. Update neighbors' distances:
For the current node, look at each of its unvisited neighbors. Calculate
the tentative distance through the current node.
If this tentative distance is smaller than the current distance for that
neighbor, update the neighbor's distance with the smaller value.
If the neighbor's distance is already smaller, leave it unchanged.
5. Repeat the process:
Repeat steps 3 and 4 until all nodes are visited or the smallest tentative
distance among the unvisited nodes is infinity (which would mean there's
no way to reach the remaining nodes).
6. Reconstruct the shortest path:
Once the algorithm finishes, you can trace the shortest path from the
target node back to the source node using the stored previous nodes.
Example:
working in class
Output: Vertex
Vertex Distance from Source
0 -> 0
1 -> 2
2 -> 6
3 -> 7
4 -> 17
5 -> 22
6 -> 19

You might also like