Escolar Documentos
Profissional Documentos
Cultura Documentos
Binary Heap
binomial heap
Fibonacci
heap
Find Min
O(1)
Log N
0(1)
Insert
O( Log N)
Log N
0(1)
Extract Min
O( Log N)
Log N
0(1)
Delete
0 (Log N)
Log N
O( Log N)
-Amortized
Decrease Key
O(Log N)
Log N
O( Log N)
-Amortized
Merge
O(n)
Log N
0(1)
Construct
O(n)
O(n)
O(n)
Amortized Cost is not average cost ,It is still the worst case.
Shortest Path: G = (V,E)
Running time O() for a path A to B or from Point A to every other point will be same (in
BFS) it will be O(|V| + |E|)
@ Is there will be any edge which if I take will be of less cost than current one ?
A No, this the given edge S-> Y is the shortest from Source S and any other edge will increase
the distance as all the distances are positive
High Level Solution :
1. A set S -> A set of shortest vertices's and u -> V-S to find the shortest distance from source
s(Think Greedy)
Define first nodes at distance of infinity and check to see if lower distance is found is called
relaxation step.
Problem Solving Principle :1. Define the problem in most abstract way
2. Define the algorithm for given problem
3. Provide proof of correctness
4. Implementation
5.Analyze the time/Space complexity
Proof of Correctness for Shortest Path Algorithm(Dijkstra's) :Proof: At each step Dijkstra's find a smallest path to the new nodes in the graph
Procedure : Mathematical Induction
Base Case :- |S| =1 and picks the source node.d(s) =0, shortest distance.
Inductive hypothesis :- lets say the above proof claim hold for |S| =k for some k >=1
If there is another path ,but that will also cross the boundary of set S and in that case Dijkstra's
will pick the node with shortest distance .
implementation :Initially S = {s} and d(s) = 0 for all the nodes d(u) = infinity .
while S != V
select a node V not in S with at least one edge from s for which d(v) = min(d(u) + le)
e(u,v) where u belong to S
Add to S
end While
Data Structure :- Priority Queue
Initialize the Priority Queue : - O(n)
Cost on n Extract Min :
Cost of m decrease key operation :
Connected : As many edges as Nodes
Operation
Binary heap
Binomial heap
Fib heap
n Extract Min
O(n log N)
O(n log N)
0(n log N)
m Decrease key
0(m log N)
0(m log N)
O(m)
If Graph is sparse ,i.e edges are in linear term of number of nodes than all are (n Log N)
if Graph is dense ,(Could have as many of n^2)
Any tree that covers all the nodes of a graph is called spanning tree
A spanning tree with minimum total edge cost is a Minimum spanning tree, or MST.
Kruskal Algorithm :Sort all the edges in the increasing order of cost.Add edge to T such that not cycle.
Connected Component :- A connected component for a graph is a subgraph with nodes that are
connected and no additional vertex is joined in that subgraph
Difference Between Kruskal and Prims Algorithm :Prims algorithm is mostly similar with Dijkstra's with node set S (initially the root node ) on
which MST has been constructed and each step we grow S by one and at each step adding a step
V that minimizes the attachment cost, => Prims Algorithms
Another way,We will remove the Edges with highest cost , unless it will remove/ disconnect the
graph (Backward Kruskal ) and if it disconnect the graph then keep those edges. => Reverse
Delete Algorithms