Question

Assume that there are 16 nodes marked 0 through 16. Initially each element should be in...

Assume that there are 16 nodes marked 0 through 16. Initially each element should be in a separate equivalence class. Hand draw the resulting trees for each input pair when you use the weighted quick-union for the sequence 1-2, 3-4, 3-5, 1-7, 3-6, 8-9, 1-8, 3-10, 3-11, 3-12, 3-13, 14-15, 16-0, 14-16, 1-3, 1-14.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code--->

import java.util.Scanner;
public class QuickUnion
{
private int[] id;//id[i] is equal to parent of i th node
private int[] sz;//sz[i] will tell how many of nodes have ith node as parent
public QuickUnion(int N)//only parameter no of nodes in graph
{
id = new int[N];//intialiy parent of i th node is i only
sz = new int[N];
for (int i = 0; i < N; i++)
{
id[i] = i;//intialiy parent of i th node is i only
sz[i]=1;//initially only one node with the i th node as porent
}
}
public int root(int i)//finding the parent of the node
{
while (i != id[i]) //parent node is the node which is parent of itself
i = id[i];
return i;
}
public boolean find(int p, int q)//if both p and q has same parent then p and q are connected else not
{
return root(p) == root(q);
}
public void unite(int p, int q)
{
int i = root(p);//find root of p
int j = root(q);//find root/parent of q
if(sz[i]<sz[j])//merge smaller tree into larger tree
{
id[i]=j;//Set the id of q's root to the id of p's root
sz[j]=sz[j]+sz[i];//update the sz[] array.
}
else
{
id[j] = i;//Set the id of p's root to the id of q's root
sz[i]=sz[i]+sz[j];//update the sz[] array.
}
}
public void print(int no_of_nodes)
{
boolean [] vis=new boolean[no_of_nodes];//visited array
int i;
for( i = 0; i < no_of_nodes; i++) //initialse visited array to false
vis[i] = false;
int count=0;
int start=0;
while(count<no_of_nodes)//While all nodes are not visited
{
System.out.print("(");
for(i=0;i<no_of_nodes;i++)
{
if(root(start)==root(i)&&vis[i]==false)//node with same root/parent will be connected
{
count++;
vis[i]=true;//mark visited as true
System.out.print(i+" ");
}
}
for(i=0;i<no_of_nodes;i++)
{
if(vis[i]==false)//search for unvisited array
{
start=i;//unconnected node to the previous start node
break;
}
}
System.out.print("),");
}
System.out.println(" ");
}
public static void main(String []args){
Scanner sc = new Scanner(System.in);
int no_of_nodes=17;//0-16
int no_of_edges = sc.nextInt(); //taking input no of edges, you can also use till end of file input
int i,j;
QuickUnion obj=new QuickUnion(no_of_nodes);//intialising class
for(i=0;i<no_of_edges;i++)//connecting graph through edges
{
int u=sc.nextInt();
int v=sc.nextInt();
obj.unite(u,v);//unite or merge the nodes of two edges
System.out.println("After merging "+ u + " and " + v);
obj.print(no_of_nodes);//print the graph
}
}
}

Input--->

16//No of edges(16 in this case)
1 2
3 4
3 5
1 7
3 6
8 9
1 8
3 10
3 11
3 12
3 13
14 15
16 0
14 16
1 3
1 14

Output:

After merging 1 and 2
( 0),( 1 2),( 3),( 4),( 5),( 6),( 7),( 8),( 9),( 10),( 11),( 12),( 13),( 14),( 15),( 16),
After merging 3 and 4
( 0),( 1 2),( 3 4),( 5),( 6),( 7),( 8),( 9),( 10),( 11),( 12),( 13),( 14),( 15),( 16),
After merging 3 and 5
( 0),( 1 2),( 3 4 5),( 6),( 7),( 8),( 9),( 10),( 11),( 12),( 13),( 14),( 15),( 16),
After merging 1 and 7
( 0),( 1 2 7),( 3 4 5),( 6),( 8),( 9),( 10),( 11),( 12),( 13),( 14),( 15),( 16),
After merging 3 and 6
( 0),( 1 2 7),( 3 4 5 6),( 8),( 9),( 10),( 11),( 12),( 13),( 14),( 15),( 16),
After merging 8 and 9
( 0),( 1 2 7),( 3 4 5 6),( 8 9),( 10),( 11),( 12),( 13),( 14),( 15),( 16),
After merging 1 and 8
( 0),( 1 2 7 8 9),( 3 4 5 6),( 10),( 11),( 12),( 13),( 14),( 15),( 16),
After merging 3 and 10
( 0),( 1 2 7 8 9),( 3 4 5 6 10),( 11),( 12),( 13),( 14),( 15),( 16),
After merging 3 and 11
( 0),( 1 2 7 8 9),( 3 4 5 6 10 11),( 12),( 13),( 14),( 15),( 16),
After merging 3 and 12
( 0),( 1 2 7 8 9),( 3 4 5 6 10 11 12),( 13),( 14),( 15),( 16),
After merging 3 and 13
( 0),( 1 2 7 8 9),( 3 4 5 6 10 11 12 13),( 14),( 15),( 16),
After merging 14 and 15
( 0),( 1 2 7 8 9),( 3 4 5 6 10 11 12 13),( 14 15),( 16),
After merging 16 and 0
( 0 16),( 1 2 7 8 9),( 3 4 5 6 10 11 12 13),( 14 15),
After merging 14 and 16
( 0 14 15 16),( 1 2 7 8 9),( 3 4 5 6 10 11 12 13),
After merging 1 and 3
( 0 14 15 16),( 1 2 3 4 5 6 7 8 9 10 11 12 13),
After merging 1 and 14
( 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16),

Add a comment
Know the answer?
Add Answer to:
Assume that there are 16 nodes marked 0 through 16. Initially each element should be in...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Assume that there are 16 nodes marked 0 through 16. Initially each element should be in...

    Assume that there are 16 nodes marked 0 through 16. Initially each element should be in a separate equivalence class. Hand draw the resulting trees for each input pair when you use the weighted quick-union for the sequence 1-2, 3-4, 3-5, 1-7, 3-6, 8-9, 1-8, 3-10, 3-11, 3-12, 3-13, 14-15, 16-0, 14-16, 1-3, 1-14. PLEASE DO NOT SIMPLY COPY SOLUTIONS FROM OTHER CHEGG POSTS, OR YOU WILL BE RATED DOWN.

  • Trees and Heaps 1. Show that the maximum number of nodes in a binary tree of...

    Trees and Heaps 1. Show that the maximum number of nodes in a binary tree of height h is 2h+1 − 1. 2. A full node is a node with two children. Prove that the number of full nodes plus one is equal to the number of leaves in a nonempty binary tree. 3. What is the minimum number of nodes in an AVL tree of height 15? 4. Show the result of inserting 14, 12, 18, 20, 27, 16,...

  • In the Benes network B4 16 inputs are matched with 16 outputs. The network can send each input to...

    In the Benes network B4 16 inputs are matched with 16 outputs. The network can send each input to either of two copies of B3 , which will be called the upper and lower copy. To have a congestion of 1 it is essential that any two inputs that differ by exactly 8 go to different copies of B3, and that any two packets with outputs that differ by exactly 8 also go to different copies of B3. Consider the...

  • • P1 (10 pts) Show the result of inserting 2, 9, 5, 8, 6, 4, 3,...

    • P1 (10 pts) Show the result of inserting 2, 9, 5, 8, 6, 4, 3, 1 into an initially empty AVL tree (draw a resulting tree after inserting each number; you need to draw 8 AVL trees). • P2 (5 pts) What is the minimum number of nodes in an AVL tree of height 8? • P3 (5 pts) Show the result of deleting the element with key 9' from the following splay tree. • P4 (5 pts) Show...

  • Please answer A and B 1. Consider the following adjacency matrix representing vertices v through v^:...

    Please answer A and B 1. Consider the following adjacency matrix representing vertices v through v^: weighted graph containing a ro 5 0 0 8 0 61 5 0 0 7 0 0 0 jo 0 0 0 0 1 3| 0 7 0 0 2 0 0 8 0 0 0 0 1 0 0 0 4 L6 0 3 0 0 4 0- 20 0 0 a. Draw the graph resulting from the adjacency matrix b. Assuming the...

  • 1. Given the two binary trees below: 14 16 Write a method called swapSubtrees, which swaps all of...

    in java ..write all complete program from a- e 1. Given the two binary trees below: 14 16 Write a method called swapSubtrees, which swaps all of the left and right subtrees in the above binary trees. Add this method to the class BinaryTree and create a program to test this method for these 2 trees. Show the original trees and the resulting trees. Note: To test your algorithm, first create a binary search tree. Write a method called singleParent,...

  • Show the contents of the array below, once the “pivot” element is placed at its appropriate...

    Show the contents of the array below, once the “pivot” element is placed at its appropriate location after each call of the “Partition” algorithm, in the process of running Quick-Sort on said array. Arrange the data in ascending order (from smallest to largest value). Always select the first element of the partition as “pivot” Apply sorting on the following data set 19,       20,       1,         13,       16,       5,         4,         9,         14,       7 Index 0 1 2 3 4 5 6 7...

  • **Only need to answer question 27** Union(2,10) - So each key above is in its own...

    **Only need to answer question 27** Union(2,10) - So each key above is in its own set so if we perform union(2,10) im assuming it should make a set that contains both key such as s1 = {2, 10} or using an array for example -1 -1 2 10 -1 meaning vertices in own set then this should result in after union as -2 2 2 10 -2 being the number of vertices, positive 2 being the parent of 10...

  • B trees java NAME CSC 236 HW #3 (B-trees & heaps) 1. Given a B-tree of...

    B trees java NAME CSC 236 HW #3 (B-trees & heaps) 1. Given a B-tree of order 5, add the elements 1, 12, 8, 2, 25, 5, 14, 28, 17, 7, 52, 16, 48, 68, 3, 26, 29, 53, 55, 45 into a B-tree in this order. Draw the diagrams to show the B-tree after each element is added. 2. Add the elements 27, 35, 23, 22, 4, 45, 21, 5, 42, 19 into a heap in this order Draw...

  • plz use python to answer it, thanks in advance 3. Tree and back arcs in a DFS 40 Marks For a given set of digraphs, write a program that performs DFS on each digraph starting at node 0 and prints...

    plz use python to answer it, thanks in advance 3. Tree and back arcs in a DFS 40 Marks For a given set of digraphs, write a program that performs DFS on each digraph starting at node 0 and prints out the total number of tree arcs and back arcs resulting from the traversal. Use our standard convention that when there is a choice of white or grey nodes, the one with the lowest index should be chosen. Input format:...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT