We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26
Biconnected Components
Bucket Radix sort
• A graph is said to be Biconnected if: – It is connected, i.e. it is possible to reach every vertex from every other vertex, by a simple path. – Even after removing any vertex the graph remains connected. – The given graph is clearly connected. – Now try removing the vertices one by one and observe. Removing any of the vertices does not increase the number of connected components. So the given graph is Biconnected. In the above graph if the vertex 2 is removed, then here's how it will look: Clearly the number of connected components have increased. Similarly, if vertex 3 is removed there will be no path left to reach vertex 0 from any of the vertices 1, 2, 4 or 5. And same goes for vertex 4 and 1. Removing vertex 4 will disconnect 1 from all other vertices 0, 2, 3 and 4. So the graph is not Biconnected. strongly connected component • A strongly connected component is the portion of a directed graph in which there is a path from each vertex to another vertex. It is applicable only on a directed graph. You can observe that in the first strongly connected component, every vertex can reach the other vertex through the directed path. Bucket sort • Bucket sort, or bin sort, is a sorting algorithm that works by distributing the elements of an array into a number of buckets. Each bucket is then sorted individually, either using a different sorting algorithm, or by recursively applying the bucket sorting algorithm The computational complexity depends on the algorithm used to sort each bucket, the number of buckets to use, and whether the input is uniformly distributed • Bucket sort works as follows: – Set up an array of initially empty "buckets". – Scatter: Go over the original array, putting each object in its bucket. – Sort each non-empty bucket. – Gather: Visit the buckets in order and put all elements back into the original array. • function bucketSort(array, k) is – buckets ← new array of k empty lists – M ← the maximum key value in the array – for i = 0 to length(array) do – insert array[i] into buckets[floor(k × array[i] / M)] – for i = 0 to k do – nextSort(buckets[i]) – return the concatenation of buckets[0], ...., buckets[k] Working of a Bucket Sort Algorithm
• Assume the input array is:
• Best Case Complexity O(n) – It occurs when the elements are distributed uniformly in the buckets, with nearly identical elements in each bucket. – When the elements within the buckets are already sorted, the complexity increases. – If insertion sort is used to sort bucket elements, the overall complexity will be linear, i.e. O(n+k). – O(n) is the complexity of creating buckets, and O(k) is the complexity of sorting bucket elements using algorithms with linear time complexity in the best case. • Average Case Complexity O(n) – It happens when the array's elements are distributed at random. – Bucket sorting takes linear time, even if the elements are not distributed uniformly. – It holds until the sum of the squares of the bucket sizes is linear in terms of the total number of elements. • Worst Case Complexity O(n*n) – When elements in the array are close in proximity, they are likely to be placed in the same bucket. – As a result, some buckets may contain more elements than others. – It makes the complexity dependent on the sorting algorithm used to sort the bucket's elements. – When the elements are placed in reverse order, the complexity increases even more. – When insertion sort is used to sort bucket elements, the time complexity becomes O (n2). Radix sort • [170, 45, 75, 90, 2, 802, 2, 66] • Starting from the rightmost (last) digit, sort the numbers based on that digit: • [{170, 90}, {2, 802, 2}, {45, 75}, {66}] • Sorting by the next left digit: • [{02, 802, 02}, {45}, {66}, {170, 75}, {90}] • Notice that an implicit digit 0 is prepended for the two 2s so that 802 maintains its position between them. • And finally by the leftmost digit: • [{002, 002, 045, 066, 075, 090}, {170}, {802}] • Example of Radix Sort Algorithm – a=[682, 244, 73, 6, 535, 123] • RadixSort(a[], n): • // Finding the maximum element • max=a[0] • For (i=1 to n-1): • If (a[i]>max): • max=a[i] • // Calling countingSort for • // k times using For loop. • For (div=1 to max/div>0): • countingSort(a, n, div) • div=div*10 • Best Case - In the best case i.e. when the array is already sorted, we do not need to apply the algorithm instead we can check if the array is sorted in a single traversal of the array. Hence time complexity is O(n)) in the best case. • Worst Case - In the worst case ii.e. array is sorted in reverse order, we need to apply the counting Sort (an O(n) process) for k times. Where k is the number of digits present in the largest element present in a. Hence, the overall time complexity is O(n×k) • Average Case - In the average case i.e.. elements of the array are arbitrarily arranged, again we need to apply counting sort on the array for k times. Hence in the average case also, the time complexity is O(n×k).