Downloadfile 1
Downloadfile 1
M
M2=S tra s s en( A21+A22,B 11)
● I t is a strategy inspired by the idea of breaking a large task into M3=S tra s s en( A11,B 12-B 22)
s maller, manageable parts. M4=S tra s s en( A22,B 21-B 11)
● I t was originally used in politics as “Divide et impera” (divide M5=S tra s s en( A11+A12,B 22)
a nd rule) by Roman rulers. M6=S tra s s en( A21-A11,B 11+B 12)
M7=S tra s s en( A12-A22,B 21+B 22)
● I n computer science, it is a method to solve complex problems
efficiently.
11=M1+M4-M5+M7
C
● Steps: C12=M3+M5
● Divide: Split the main problem into smaller, C21=M2+M4
non-overlapping subproblems. C22=M1-M2+M3+M6
● Conquer: Solve each of these smaller subproblems
(usually using recursion). ombi neC11,C12,C21,C22i nto C
C
● Combine: Merge the solutions of the subproblems tos olve returnC
the original problem. Representation of Graph
1.Adjacency Matrix
2.Linked list
Graph Traversal
Algorithm ● G raph is represented by its nodes and edges, so traversal of
S tra s s en( A,B ) : each node is the traversing in graph.
i fs i ze( A) ==1: ● There are two standard ways of traversing a graph1.Depth first
returnA[0][0]*B [0
][0] search 2. Breadth first search
i vi de A i nto A11,A12,A21,A22
D
D i vi de B i nto B11,B 12,B 21,B 22
DFS Adjacency List:
Numerical of BFS
Kosaraju Algorithm Activity Selection Problem
Kosaraju(G):
● T he Activity Selection Problem is about selecting the maximum
1.S ta ck=[]
2.R unD FS1o n G to fil l Sta ck by fini s h time
number of activities that do not overlap, given their start and
3.Tra ns pos e G to get G^T finish times.
4.S CCs =[] ● Problem Statement
5.Whi l e Sta cki s n ote mpty: ● You are givenn activities, each with a start time(s[i])a nd a
Pop vfromS ta ck finish time(f[i]).
I f vi s u nvi s i tedi nG ^T: ● Choose the maximum number of activities that can be
R unD FS2o n G^T sta rtingfromv performed without overlapping.
Addallvi s i ted nodes to SCCs
6.Return SCCs
Algorithm for activity Selection Problem
S ort the activities by their finish times.
●
● Select the first activity (the one that finishes earliest).
Convex Hull ● For each subsequent activity, check if its start time is greater
● T he convex hull of a set of points in a 2D plane is the smallest than or equal to the finish time of the last selected activity.
convex polygon that encloses all the given points. ● I f yes, select the activity.
● I magine stretching a rubber band around the points—it snaps
i nto the shape of the convex hull. Example
● The convex hull is a convex polygon, meaning any line segment
between two points inside the hull lies entirely within it. ● Input:
● I t contains all the given points either on its boundary or inside. Activities = (1,3),(2,5),(4,6),(6,8),(5,7) (Each pair represents
(start,finish))
● Steps:
● Sort activities by finish times: (1,3),(4,6),(2,5),(5,7),(6,8)
● Select (1,3).
● Next non-overlapping activities: (4,6), (6,8)
utput:
O
Selected activities: (1,3),(4,6),(6,8)
Applications Time Complexity:
C
● omputer Graphics:Shape modeling and collision detection. S orting: O(nlogn)
●
● Geographic Information Systems (GIS):Bounding geographical ● Selection: O(n)
data. ● Total: O(nlogn) .
● Robotics: Path planning and motion trajectories.
● Machine Learning: Support vector machines and dataclustering. Applications:
S cheduling tasks.
●
● Allocating resources.
Example
● Given points: (1,1),(2,2),(2,0),(2,4),(3,3),(4,2) Quantum 3.13
● The convex hull includes points: (1,1),(2,4),(4,2),(2,0) Fractional Knapsack Problem
forming a quadrilateral.
● T he Fractional Knapsack Problem involves maximizing the total
Greedy Algorithms value of items placed in a knapsack with a given weight
capacity.
G
● reedy algorithms are simple and easy to understand.
● Unlike the 0/1 Knapsack Problem, you can take fractions of an
● They make decisions based only on the current situation,
i tem.
without thinking about how those decisions might affect future.
● Example Problem
● They are easy to create, simple to code, and often very fast.
● Given:
● However, greedy algorithms don't always give the correct
● I tems: (v,w)=[(60,10),(100,20),(120,30)]
s olution for every problem.
● Knapsack capacity W=50
● They are mostly used for solving optimization problems.
Advantages
S imple to implement.
●
● Efficient for many problems, with time complexity often O(nlogn)
or better.
Limitations
● G
reedy algorithms may fail when future choices depend on
earlier ones, like in the knapsack problem where fractional Seen
choices are not allowed.
“Unlike the Fractional Knapsack Problem, where items can be divided,
in the 0/1 Knapsack Problem, each item can either be taken whole or
left out.”
Quantum 3.22
Prims Algorithm Algorithm Step
Initialize:Create a forestw itheach vertexasa separate tree.
● I t is a greedy algorithm that is used to find the minimum SortEdges:Arrangealledgesinincreasing orderof their weights.
s panning tree from a graph. B uildMST:Pick the smallest edge.If it doesn't forma cycle, include it in the
● Prim's algorithm starts with the single node and explores all MST.
the adjacent nodes with all the connecting edges at every step. Repeat:Continue until theMSThas 𝑉−1edges( w hereVisthe number of
● I t is used for undirected graph . vertices) .
● Time complexity :O(E Log V)) & space complexity : O(V)
Dijkstra algorithm
● D
ijkstra's algorithm is an algorithm for finding the shortest
paths between nodes in a weighted graph
● I t is a type of Greedy Algorithm that only works on Weighted
Graphs having positive weights.
● I t can also be used for finding the shortest paths from a single
node to a single destination
● time complexity : O( E log V) & space complexity :O( V)
Algorithm Step
I m
plementation of BellmanFord Algortihm
Algorithm Steps
1.Initializedistances:
dist[source]=0
dist[allother vertices]=infinity