0% found this document useful (0 votes)
129 views6 pages

Huffman Codes: Spanning Tree

The document discusses various algorithms related to graphs and strings: 1. The greedy method is an optimization technique that makes locally optimal choices at each step to find a global optimum. Huffman coding is an example of a greedy algorithm. 2. Kruskal's and Prim's algorithms are greedy approaches for finding minimum spanning trees in weighted graphs. 3. Dijkstra's and Bellman-Ford algorithms solve the single-source shortest path problem on graphs, with Bellman-Ford handling some negative edge weights. 4. The Rabin-Karp algorithm improves on the naive string matching approach by using hashing to avoid unnecessary character comparisons. The Knuth-Morris-Pratt algorithm

Uploaded by

Bhartiya Nagrik
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
129 views6 pages

Huffman Codes: Spanning Tree

The document discusses various algorithms related to graphs and strings: 1. The greedy method is an optimization technique that makes locally optimal choices at each step to find a global optimum. Huffman coding is an example of a greedy algorithm. 2. Kruskal's and Prim's algorithms are greedy approaches for finding minimum spanning trees in weighted graphs. 3. Dijkstra's and Bellman-Ford algorithms solve the single-source shortest path problem on graphs, with Bellman-Ford handling some negative edge weights. 4. The Rabin-Karp algorithm improves on the naive string matching approach by using hashing to avoid unnecessary character comparisons. The Knuth-Morris-Pratt algorithm

Uploaded by

Bhartiya Nagrik
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

The greedy method is one of the strategies like Divide and conquer used to solve the problems.

This
method is used for solving optimization problems. An optimization problem is a problem that demands
either maximum or minimum results
The main function of this approach is that the decision is taken on the basis of the currently available
information. Whatever the current information is present, the decision is made without worrying about the
effect of the current decision in future.
This technique is basically used to determine the feasible solution that may or may not be optimal.

Huffman Codes
o (i) Data can be encoded efficiently using Huffman Codes.
o (ii) It is a widely used and beneficial technique for compressing data.
o (iii) Huffman's greedy algorithm uses a table of the frequencies of occurrences of each character to
build up an optimal way of representing each character as a binary string.
o Huffman (C)
o 1. n=|C|
o 2. Q ← C
o 3. for i=1 to n-1
o 4. do
o 5. z= allocate-Node ()
o 6. x= left[z]=Extract-Min(Q)
o 7. y= right[z] =Extract-Min(Q)
o 8. f [z]=f[x]+f[y]
o 9. Insert (Q, z)
o 10. return Extract-Min (Q)

A tree is a graph with the following properties:

1. The graph is connected (can go from anywhere to anywhere)


2. There are no cyclic (Acyclic)

Spanning Tree:
Given a connected undirected graph, a spanning tree of that graph is a subgraph that is a tree and
joined all vertices. A single graph can have many spanning trees.

Properties of Spanning Tree:

1. There may be several minimum spanning trees of the same weight having the minimum
number of edges.
2. If all the edge weights of a given graph are the same, then every spanning tree of that
graph is minimum.
3. If each edge has a distinct weight, then there will be only one, unique minimum spanning
tree.
4. A connected graph G can have more than one spanning trees.
5. A disconnected graph can't have to span the tree, or it can't span all the vertices.
6. Spanning Tree doesn't contain cycles.
7. Spanning Tree has (n-1) edges where n is the number of vertices.

Addition of even one single edge results in the spanning tree losing its property of Acyclicity and
elimination of one single edge results in its losing the property of connectivity.

Minimum Spanning Tree:


Minimum Spanning Tree is a Spanning Tree which has minimum total cost. If we have a
linked undirected graph with a weight (or cost) combine with each edge. Then the cost of
spanning tree would be the sum of the cost of its edges.

Kruskal's Algorithm:
An algorithm to construct a Minimum Spanning Tree for a connected weighted graph. It is a
Greedy Algorithm.

If the graph is not linked, then it finds a Minimum Spanning Tree.

1. Arrange the edge of G in order of increasing weight.


2. Starting only with the vertices of G and proceeding sequentially add each edge which does
not result in a cycle, until (n - 1) edges are used.
3. EXIT.

Analysis: Where E is the number of edges in the graph and V is the number of vertices, Kruskal's
Algorithm can be shown to run in O (E log E) time, or simply, O (E log V) time, all with simple data
structures. These running times are equivalent because:

Prim's Algorithm
It is a greedy algorithm. It starts with an empty spanning tree. The idea is to maintain two sets of
vertices:

o Contain vertices already included in MST.


o Contain vertices not yet included.

At every step, it considers all the edges and picks the minimum weight edge. After picking the
edge, it moves the other endpoint of edge to set containing MST.

Steps for finding MST using Prim's Algorithm:


1. Create MST set that keeps track of vertices already included in MST.
2. Assign key values to all vertices in the input graph. Initialize all key values as INFINITE (∞). Assign
key values like 0 for the first vertex so that it is picked first.
3. While MST set doesn't include all vertices.

1. Pick vertex u which is not is MST set and has minimum key value. Include 'u'to MST set.
2. Update the key value of all adjacent vertices of u. To update, iterate through all adjacent
vertices. For every adjacent vertex v, if the weight of edge u.v less than the previous key
value of v, update key value as a weight of u.v.

SINGLE SOURCE SHORTEST PATH


The breadth-first- search algorithm is the shortest path algorithm that works on unweighted
graphs, that is, graphs in which each edge can be considered to have unit weight.

Dijkstra Algorithm
Dijkstra algorithm is a single-source shortest path algorithm. Here, single-source means that only
one source is given, and we have to find the shortest path from the source to all the nodes.

Dijkstra's Algorithm works on the basis that any subpath  B -> D  of the shortest path  A ->

D  between vertices A and D is also the shortest path between vertices B and D.
we overestimate the distance of each vertex from the starting vertex. Then we visit
each node and its neighbors to find the shortest subpath to those neighbors.

The algorithm uses a greedy approach in the sense that we find the next best solution
hoping that the end result is the best solution for the whole problem.

Dijkstra's Algorithm Complexity


Time Complexity:  O(E Log V)

where, E is the number of edges and V is the number of vertices.

Space Complexity:  O(V)

Bellman-Ford Algorithm
Solves single shortest path problem in which edge weight may be negative but no negative cycle
exists.

This algorithm works correctly when some of the edges of the directed graph G may have negative
weight. When there are no cycles of negative weight, then we can find out the shortest path
between source and destination.
It is slower than Dijkstra's Algorithm but more versatile, as it capable of handling some of the
negative weight edges.

This algorithm detects the negative cycle in a graph and reports their existence.

NAIVE-STRING-MATCHER (T, P)
1. n ← length [T]
2. m ← length [P]
3. for s ← 0 to n -m
4. do if P [1.....m] = T [s + 1....s + m]
5. then print "Pattern occurs with shift" s
The Rabin-Karp string matching algorithm calculates a hash value for the pattern, as well as for each M-
character subsequences of text to be compared. If the hash values are unequal, the algorithm will determine
the hash value for next M-character sequence. If the hash values are equal, the algorithm will analyze the
pattern and the M-character sequence. In this way, there is only one comparison per text subsequence, and
character matching is only required when the hash values match.

RABIN-KARP-MATCHER (T, P, d, q)
1. n ← length [T]
2. m ← length [P]
3. h ← dm-1 mod q
4. p ← 0
5. t0 ← 0
6. for i ← 1 to m
7. do p ← (dp + P[i]) mod q
8. t0 ← (dt0+T [i]) mod q
9. for s ← 0 to n-m
10. do if p = ts
11. then if P [1.....m] = T [s+1.....s + m]
12. then "Pattern occurs with shift" s
13. If s < n-m
14. then ts+1 ← (d (ts-T [s+1]h)+T [s+m+1])mod q

worst case scenario O ((n-m+1) m

If the expected number of strong shifts is small O (1) and prime q is chosen to be quite large, then the
Rabin-Karp algorithm can be expected to run in time O (n+m) plus the time to require to process spurious
hits.

Knuth-Morris and Pratt introduce a linear time algorithm for the string matching problem. A matching
time of O (n) is achieved by avoiding comparison with an element of 'S' that have previously been involved
in comparison with some element of the pattern 'p' to be matched.
Components of KMP Algorithm:
1. The Prefix Function (Π): The Prefix Function, Π for a pattern encapsulates knowledge about
how the pattern matches against the shift of itself. This information can be used to avoid a useless
shift of the pattern 'p.' In other words, this enables avoiding backtracking of the string 'S.'

2. The KMP Matcher: With string 'S,' pattern 'p' and prefix function 'Π' as inputs, find the
occurrence of 'p' in 'S' and returns the number of shifts of 'p' after which occurrences are found.

Definition of NP class Problem: - The set of all decision-based problems came into the division of NP
Problems who can't be solved or produced an output within polynomial time but verified in the polynomial
time. NP class contains P class as a subset. NP problems being hard to solve.

Definition of P class Problem: - The set of decision-based problems come into the division of P
Problems who can be solved or produced an output within polynomial time. P problems being
easy to solve

Definition of Polynomial time: - If we produce an output according to the given input within a
specific amount of time such as within a minute, hours. This is known as Polynomial time.

Definition of Non-Polynomial time: - If we produce an output according to the given input but
there are no time constraints is known as Non-Polynomial time. But yes output will produce but
time is not fixed yet.

Definition of NP-hard class: - Here you to satisfy the following points to come into the division
of NP-hard

1. If we can solve this problem in polynomial time, then we can solve all NP problems in
polynomial time
2. If you convert the issue into one form to another form within the polynomial time

Definition of NP-complete class: - A problem is in NP-complete, if

1. It is in NP
2. It is NP-hard

NP: is the set of decision problems that can be verified in polynomial time.

NP-Hard: L is NP-hard if for all L' ϵ NP, L' ≤p L. Thus if we can solve L in polynomial time, we can
solve all NP problems in polynomial time.

NP-Complete L is NP-complete if

1. L ϵ NP and
2. L is NP-hard

You might also like