Professional Documents
Culture Documents
In Sem 1 Key - Set 1 - AOOP - 21CS2116AA-16-38
In Sem 1 Key - Set 1 - AOOP - 21CS2116AA-16-38
In Sem 1 Key - Set 1 - AOOP - 21CS2116AA-16-38
2
clear( ) Removes all objects from the collection.
5 iterator( ) Returns an Iterator object for the collection, which may be used to
retrieve an object.
Example
Set has its implementation in various classes like HashSet, TreeSet, LinkedHashSet. Following
is an example to explain Set functionality
import java.util.*;
public class SetDemo {
Output
Maps:
The map interface is present in java.util package represents a mapping between a key and a
value.
merge(K key, V value, If the specified key is not already associated with a
BiFunction<? super V,? super V,? value or is associated with null, associates it with the
extends V> remappingFunction) given non-null value.
Sample program:
Output
vaibhav 20
vishal 10
sachin 30
Following are the operations to be performed in above mentioned 4 cases. In all of the
cases, we only need to re-balance the subtree rooted with z and the complete tree becomes
balanced as the height of the subtree (After appropriate rotations) rooted with z becomes
the same as it was before insertion.
z z x
/\ / \ / \
y T4 Left Rotate (y) x T4 Right Rotate(z) y z
/ \ - - - - - - - - -> / \ - - - - - - - -> / \ / \
T1 x y T3 T1 T2 T3 T4
/\ /\
T2 T3 T1 T2
z y
/ \ / \
T1 y Left Rotate(z) z x
/ \ - - - - - - - -> / \ / \
T2 x T1 T2 T3 T4
/\
T3 T4
z z x
/\ /\ / \
T1 y Right Rotate (y) T1 x Left Rotate(z) z y
/ \ - - - - - - - - -> / \ - - - - - - - -> / \ / \
x T4 T2 y T1 T2 T3 T4
/\ / \
T2 T3 T3 T4
Kruskal algorithm is another most important algorithm used for Minimum Spanning Tree.
MST is a spanning tree having a weight less than or equal to the weight of every spanning
tree.
Kruskal algorithm in Java takes a connected and undirected graph and returns the Minimum
Spanning Tree of it. The given diagram defines the working of Kruskal's algorithm.
These are the following steps that we use to implement Kruskal's algorithm:
Prim's algorithm in Java is one of the most used algorithms for Minimum Spanning Tree
. Prim's algorithm starts with a spanning tree having no vertices. In prim's algorithm, we
maintain two sets of vertices in which first contains those vertices that are present in the
MSP and the second one contains those vertices that are not present in the MSP.
At each step, it considers all edges that combine the two sets and selects the minimum
weight edge from these edges. After selecting an edge, it sets the other end of the edge that
contains the MST.
Prim's Algorithm
• Create any collection that contains only unique elements keeps track of vertices
already included in MST.
• For all the vertices of the input graph, assign a key-value pair and set the value to
infinite. In order to pick the first vertex, we set its key value as 0.
• Select a vertex u that is not present in the setOfMST and having a minimum key
value.
• Add vertex u to the setOfMST.
• Change the key value of all adjacent vertices of u.
Note: In order to update the key-value of the adjacent vertices v, if the weight of edge u-v is
less than the previous key value of v, change the key value as the weight of u-v.
• Repeat steps 3, 4, and 5 until the setOfMST doesn't contain all vertices.
• Suppose the cities are x1 x2..... xn where cost cij denotes the cost of travelling from
city xi to xj. The travelling salesperson problem is to find a route starting and ending
at x1 that will take in all cities with the minimum cost.
• Example: A newspaper agent daily drops the newspaper to the area assigned in such
a manner that he has to cover all the houses in the respective area with minimum
travel cost. Compute the minimum travel cost.
•
• Solution: The cost- adjacency matrix of graph G is as follows:
• costij =
•
• The tour starts from area H1 and then select the minimum cost area reachable from
H1
•
• Mark area H6 because it is the minimum cost area reachable from H1 and then select
minimum cost area reachable from H6.
•
• Mark area H7 because it is the minimum cost area reachable from H6 and then select
minimum cost area reachable from H7.
•
•
•
•
•
•
•
• Mark area H8 because it is the minimum cost area reachable from H8.
•
•
• Mark area H5 because it is the minimum cost area reachable from H5.
•
• Mark area H2 because it is the minimum cost area reachable from H2.
•
• Mark area H3 because it is the minimum cost area reachable from H3.
•
• Mark area H4 and then select the minimum cost area reachable from H4 it is H1.So,
using the greedy strategy, we get the following.
• 4 3 2 4 3 2 1 6
• H1 → H6 → H7 → H8 → H5 → H2 → H3 → H4 → H1.
•
• Thus the minimum travel cost = 4 + 3 + 2 + 4 + 3 + 2 + 1 + 6 = 25