-
-
Notifications
You must be signed in to change notification settings - Fork 48.7k
solved the issue #12046 and #12020 #12057
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
d65aecf
7eb6984
24bcc2e
f0f2213
d3402ee
d425012
8e56553
b492d5f
9e8d831
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# Function to perform partition of the array | ||
def partition(arr, low, high): | ||
# Choose the last element as the pivot | ||
pivot = arr[high] | ||
|
||
# Pointer for greater element | ||
i = low - 1 # index of smaller element | ||
|
||
# Traverse through all elements | ||
for j in range(low, high): | ||
# If the current element is smaller than or equal to the pivot | ||
if arr[j] <= pivot: | ||
i = i + 1 # Increment the index of smaller element | ||
arr[i], arr[j] = arr[j], arr[i] # Swap | ||
|
||
# Swap the pivot element with the element at i+1 | ||
arr[i + 1], arr[high] = arr[high], arr[i + 1] | ||
|
||
# Return the partition point | ||
return i + 1 | ||
|
||
|
||
# Function to implement Quick Sort | ||
def quick_sort(arr, low, high): | ||
|
||
if low < high: | ||
# Find the partition index | ||
pi = partition(arr, low, high) | ||
|
||
# Recursively sort the elements before and after partition | ||
quick_sort(arr, low, pi - 1) # Before partition | ||
quick_sort(arr, pi + 1, high) # After partition | ||
|
||
|
||
# Driver code to take user-defined input and sort | ||
if __name__ == "__main__": | ||
# Ask the user for input | ||
n = int(input("Enter the number of elements in the array: ")) | ||
|
||
# Input array elements from the user | ||
arr = list(map(int, input(f"Enter {n} elements separated by spaces: ").split())) | ||
|
||
print("Original array:", arr) | ||
|
||
# Call quick sort function | ||
quick_sort(arr, 0, len(arr) - 1) | ||
|
||
# Print sorted array | ||
print("Sorted array:", arr) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
# Class to represent a graph | ||
class Graph: | ||
def __init__(self, vertices): | ||
|
||
self.V = vertices # Number of vertices | ||
self.graph = [] # List to store graph edges (u, v, w) | ||
|
||
# Function to add an edge to the graph (u -> v with weight w) | ||
def add_edge(self, u, v, w): | ||
|
||
self.graph.append([u, v, w]) | ||
|
||
# Utility function to find set of an element i (uses path compression) | ||
def find(self, parent, i): | ||
|
||
if parent[i] == i: | ||
return i | ||
return self.find(parent, parent[i]) | ||
|
||
# Function that does union of two sets of x and y (uses union by rank) | ||
def union(self, parent, rank, x, y): | ||
|
||
root_x = self.find(parent, x) | ||
root_y = self.find(parent, y) | ||
|
||
# Attach smaller rank tree under the root of the high-rank tree | ||
if rank[root_x] < rank[root_y]: | ||
parent[root_x] = root_y | ||
elif rank[root_x] > rank[root_y]: | ||
parent[root_y] = root_x | ||
else: | ||
parent[root_y] = root_x | ||
rank[root_x] += 1 | ||
|
||
# Main function to construct MST using Kruskal's algorithm | ||
def kruskal_mst(self): | ||
|
||
# This will store the resultant Minimum Spanning Tree (MST) | ||
result = [] | ||
|
||
# Step 1: Sort all edges in non-decreasing order of their weight | ||
# If we are using a greedy algorithm, we need to sort the edges first | ||
self.graph = sorted(self.graph, key=lambda item: item[2]) | ||
|
||
# Allocate memory for creating V subsets (for the disjoint-set) | ||
parent = [] | ||
rank = [] | ||
|
||
# Create V subsets with single elements | ||
for node in range(self.V): | ||
parent.append(node) | ||
rank.append(0) | ||
|
||
# Number of edges in MST is V-1, so we will stop once we have V-1 edges | ||
e = 0 # Initialize result edges count | ||
i = 0 # Initialize the index for sorted edges | ||
|
||
# Loop until MST has V-1 edges | ||
while e < self.V - 1: | ||
# Step 2: Pick the smallest edge | ||
#increment the index for the next iteration | ||
u, v, w = self.graph[i] | ||
i = i + 1 | ||
|
||
# Step 3: Find sets of both vertices u and v | ||
x = self.find(parent, u) | ||
y = self.find(parent, v) | ||
|
||
# If adding this edge doesn't cause a cycle, include it in the result | ||
if x != y: | ||
result.append([u, v, w]) | ||
e = e + 1 # Increment the count of edges in the MST | ||
self.union(parent, rank, x, y) | ||
|
||
# Else, discard the edge (it would create a cycle) | ||
|
||
# Print the constructed Minimum Spanning Tree | ||
print("Following are the edges in the constructed MST:") | ||
for u, v, w in result: | ||
print(f"{u} -- {v} == {w}") | ||
|
||
|
||
# Example usage | ||
if __name__ == "__main__": | ||
V = int( | ||
input("Enter the number of vertices: ") | ||
) # Ask user for the number of vertices | ||
E = int(input("Enter the number of edges: ")) # Ask user for the number of edges | ||
|
||
g = Graph(V) # Create a graph with V vertices | ||
|
||
# Ask user to input the edges in the format u v w | ||
print("Enter each edge in the format: vertex1 vertex2 weight") | ||
for _ in range(E): | ||
u, v, w = map(int, input().split()) # Take input for edge (u, v) with weight w | ||
g.add_edge(u, v, w) | ||
|
||
# Print the constructed MST | ||
g.kruskal_mst() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As there is no test file in this pull request nor any test function or class in the file
divide_and_conquer/quicksort.py
, please provide doctest for the functionpartition
Please provide return type hint for the function:
partition
. If the function does not return a value, please provide the type hint as:def function() -> None:
Please provide type hint for the parameter:
arr
Please provide type hint for the parameter:
low
Please provide type hint for the parameter:
high