In this article, we will generate test cases such that given set edges form a Tree. Below are the two conditions of the Tree:
- It should have one less edge than the number of vertices.
- There should be no cycle in it.
Approach: The idea is to run a loop and add one edge each time that is generated randomly, and for adding each edge we will check whether it is forming cycle or not. We can ensure that cycle is present or not with the help of Disjoint-Set Union. If adding any edges form cycle then ignore the current edges and generate the new set of edges. Else print the edges with randomly generated weight.
Below is the implementation of the above approach:
1 10 4 2 67 8 3 64 6 5 31 7 6 77 8 2 64 9 2 44 5 9 10 1 6 71 10 7 32
Time Complexity: O(N*logN)
Auxiliary Space: O(1)
- Union-Find Algorithm | (Union By Rank and Find by Optimized Path Compression)
- Random Tree Generator Using Prüfer Sequence with Examples
- Test Case Generation | Set 2 ( Random Characters, Strings and Arrays of Random Strings)
- Test Case Generation | Set 4 (Random directed / undirected weighted and unweighted Graphs)
- Test Case Generation | Set 1 (Random Numbers, Arrays and Matrices)
- Test Case Generation | Set 3 (Unweighted and Weighted Trees)
- Test Case Generation | Set 5 (Generating random Sorted Arrays and Palindromes)
- Disjoint Set Union on trees | Set 1
- Disjoint Set Union on trees | Set 2
- Fibonacci Heap - Insertion and Union
- Extended Disjoint Set Union on Trees
- Complexity of different operations in Binary tree, Binary Search Tree and AVL tree
- Maximum sub-tree sum in a Binary Tree such that the sub-tree is also a BST
- Print a case where the given sorting algorithm fails
- Convert a Binary Tree to Threaded binary tree | Set 1 (Using Queue)
- Order statistic tree using fenwick tree (BIT)
- Construct Full Binary Tree using its Preorder traversal and Preorder traversal of its mirror tree
- Binary Tree to Binary Search Tree Conversion using STL set
- Check if a binary tree is subtree of another binary tree using preorder traversal : Iterative
- Interval Tree using GNU Tree-based container
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.