Skip to content
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions divide_and_conquer/quicksort.py
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 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 function partition

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

# 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):

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 function quick_sort

Please provide return type hint for the function: quick_sort. 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

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)
94 changes: 94 additions & 0 deletions graphs/kruskal.py
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):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide return type hint for the function: __init__. If the function does not return a value, please provide the type hint as: def function() -> None:

Please provide type hint for the parameter: 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):

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 graphs/kruskal.py, please provide doctest for the function add_edge

Please provide return type hint for the function: add_edge. If the function does not return a value, please provide the type hint as: def function() -> None:

Please provide descriptive name for the parameter: u

Please provide type hint for the parameter: u

Please provide descriptive name for the parameter: v

Please provide type hint for the parameter: v

Please provide descriptive name for the parameter: w

Please provide type hint for the parameter: w

self.graph.append([u, v, w])

# Utility function to find set of an element i (uses path compression)
def find(self, parent, i):

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 graphs/kruskal.py, please provide doctest for the function find

Please provide return type hint for the function: find. If the function does not return a value, please provide the type hint as: def function() -> None:

Please provide type hint for the parameter: parent

Please provide descriptive name for the parameter: i

Please provide type hint for the parameter: 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):

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 graphs/kruskal.py, please provide doctest for the function union

Please provide return type hint for the function: union. If the function does not return a value, please provide the type hint as: def function() -> None:

Please provide type hint for the parameter: parent

Please provide type hint for the parameter: rank

Please provide descriptive name for the parameter: x

Please provide type hint for the parameter: x

Please provide descriptive name for the parameter: y

Please provide type hint for the parameter: 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):

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 graphs/kruskal.py, please provide doctest for the function kruskal_mst

Please provide return type hint for the function: kruskal_mst. If the function does not return a value, please provide the type hint as: def function() -> None:

# 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

Check failure on line 55 in graphs/kruskal.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W291)

graphs/kruskal.py:55:45: W291 Trailing whitespace
#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

Check failure on line 60 in graphs/kruskal.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W291)

graphs/kruskal.py:60:57: W291 Trailing whitespace
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()
Loading