Data Structure 25 + 26
Data Structure 25 + 26
STRUCTURE
Lecture 25 + 26
Instructor: Tayyba khalid
Content:
Graph algorithms
Topological sort
Greedy Algorithms
Breadth-first and Depth-first traversal
Shortest path
Graphs
■ Directed Graph: 5
■ Weighted Graph. 6 8
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
■ Working in Class.
Applications of Topological Sort:
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
Total weight= 4 + 8 + 1 + 2
+4+2+7+9 = 3
Breadth first search
■ 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.
■ 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
BFS stands for Breadth First DFS stands for Depth First
Stands for Search. Search.
BFS builds the tree level by DFS builds the tree sub-tree by
Conceptual Difference level. sub-tree.
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