From 30a1812ff17fb79df2b0e408d4dc03712acb971e Mon Sep 17 00:00:00 2001 From: Prabhat <87269080+Prabhatsingh001@users.noreply.github.com> Date: Mon, 21 Oct 2024 17:46:26 +0530 Subject: [PATCH 1/3] added bellman ford algorithm --- .../bellman_ford/Bellman_ford.py | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 Algorithms_and_Data_Structures/bellman_ford/Bellman_ford.py diff --git a/Algorithms_and_Data_Structures/bellman_ford/Bellman_ford.py b/Algorithms_and_Data_Structures/bellman_ford/Bellman_ford.py new file mode 100644 index 00000000..bc9d253f --- /dev/null +++ b/Algorithms_and_Data_Structures/bellman_ford/Bellman_ford.py @@ -0,0 +1,56 @@ +def BellmanFord(src, V, graph): + ''' + this algorithm works exactly like dijkistra algorthm except it + can detect negative edge cycle in the graph + + this algo is little slower than dijkistra algortihm + ''' + #src is source vertex V is no of vertex graph is graph in the form of a list of list + dist = [float("Inf")]*V + dist[src] = 0 + + for _ in range(V-1): + for u,v,w in graph: + if dist[u] != float("Inf") and dist[u] + w < dist[v]: + dist[v] = dist[u] + w + + + for u,v,w in graph: + if dist[u] != float("Inf") and dist[u] + w < dist[v]: + return -1 + + return dist + +def printArr(dist): + V = len(dist) + print("vertex distance from source") + for i in range(V): + print(f"{i}\t\t{dist[i]}") + +#example usage of bellman ford algorithm +def main(): + v = 5 + graph = [] + graph.append([0,1,-1]) #[u=initial vertex, final vertex, w = weight of edge btw them] + graph.append([0,2,4]) + graph.append([1,2,3]) + graph.append([1,3,2]) + graph.append([1,4,2]) + graph.append([3,2,5]) + graph.append([3,1,1]) + graph.append([4,3,-3]) + + dist = BellmanFord(0,v,graph) + printArr(dist) + +if __name__ == "__main__": + main() + +'''output +vertex distance from source +0 0 +1 -1 +2 2 +3 -2 +4 1 +''' From 4114d28aa86c4f9ad500fff7d6eb6380399b5b65 Mon Sep 17 00:00:00 2001 From: Prabhat <87269080+Prabhatsingh001@users.noreply.github.com> Date: Thu, 24 Oct 2024 20:09:06 +0530 Subject: [PATCH 2/3] added graham_scan algo --- .../graham_scan/convex_hull_graham_scan.py | 102 ++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 Algorithms_and_Data_Structures/graham_scan/convex_hull_graham_scan.py diff --git a/Algorithms_and_Data_Structures/graham_scan/convex_hull_graham_scan.py b/Algorithms_and_Data_Structures/graham_scan/convex_hull_graham_scan.py new file mode 100644 index 00000000..c978f578 --- /dev/null +++ b/Algorithms_and_Data_Structures/graham_scan/convex_hull_graham_scan.py @@ -0,0 +1,102 @@ +from functools import cmp_to_key + +class Point: + def __init__(self,x=None,y=None): + self.x = x + self.y = y + +p0 = Point(0,0) + +def nextToTop(s): + return s[-2] + +def distSq(p1,p2): + return ((p1.x - p2.x)*(p1.x - p2.x) + (p1.y - p2.y)*(p1.y - p2.y)) + +''' +To find orientation of ordered triplet (p, q, r). +The function returns following values +0 --> p, q and r are collinear +1 --> Clockwise +2 --> Counterclockwise +''' + +def orientation(p,q,r): + val = ((q.y - p.y)*(r.x - q.x) - (q.x - p.x)*(r.y-q.y)) + + if val == 0: + return 0 + elif val > 0: + return 1 + else: + return 2 + +def compare(p1,p2): + ''' + function that checks orientation + ''' + + o = orientation(p0,p1,p2) + if o == 0: + if distSq(p0,p2) >= distSq(p0,p1): + return -1 + else: + return 1 + else: + if o == 2: + return -1 + else: + return 1 + +def convexHull(points,n): + ymin = points[0].y + min = 0 + for i in range(1,n): + y = points[i].y + + if((y 1) and + (orientation(nextToTop(S), S[-1], points[i]) != 2)): + S.pop() + S.append(points[i]) + + while S: + p = S[-1] + print("(" + str(p.x) + ", " + str(p.y) + ")") + S.pop() + + +#example usage +input_points = [(0, 3), (1, 1), (2, 2), (4, 4),(0, 0), (1, 2), (3, 1), (3, 3)] +points = [] +for point in input_points: + points.append(Point(point[0], point[1])) +n = len(points) +convexHull(points, n) From 5a1cb91748137c93748047d89ef10a2b43c4dc7d Mon Sep 17 00:00:00 2001 From: Prabhat <87269080+Prabhatsingh001@users.noreply.github.com> Date: Thu, 24 Oct 2024 20:18:33 +0530 Subject: [PATCH 3/3] closest pair of points algorithm --- .../closest_pair_of_points.py | 98 +++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 Algorithms_and_Data_Structures/closest_pair_of_points/closest_pair_of_points.py diff --git a/Algorithms_and_Data_Structures/closest_pair_of_points/closest_pair_of_points.py b/Algorithms_and_Data_Structures/closest_pair_of_points/closest_pair_of_points.py new file mode 100644 index 00000000..82e0b52a --- /dev/null +++ b/Algorithms_and_Data_Structures/closest_pair_of_points/closest_pair_of_points.py @@ -0,0 +1,98 @@ +import math + +class Point: + def __init__(self, x, y): + self.x = x + self.y = y + +#sort array of points according to X coordinate +def compareX(a, b): + p1, p2 = a, b + return (p1.x != p2.x) * (p1.x - p2.x) + (p1.y - p2.y) + +#sort array of points according to Y coordinate +def compareY(a, b): + p1, p2 = a, b + return (p1.y != p2.y) * (p1.y - p2.y) + (p1.x - p2.x) + + +# A utility function to find the distance between two points +def dist(p1, p2): + return math.sqrt((p1.x - p2.x)**2 + (p1.y - p2.y)**2) + +'''A Brute Force method to return the smallest distance between two points +in P[] of size n''' +def bruteForce(P, n): + min = float('inf') + for i in range(n): + for j in range(i+1, n): + if dist(P[i], P[j]) < min: + min = dist(P[i], P[j]) + return min + +# A utility function to find a minimum of two float values +def min(x, y): + return x if x < y else y + +'''# A utility function to find the distance between the closest points of strip of a given size. All points in strip[] are sorted according to +y coordinate. They all have an upper bound on minimum distance as d.Note that this method seems to be a O(n^2) method, but it's a O(n) method as the inner loop runs at most 6 times''' +def stripClosest(strip, size, d): + min = d + for i in range(size): + for j in range(i+1, size): + if (strip[j].y - strip[i].y) < min: + if dist(strip[i],strip[j]) < min: + min = dist(strip[i], strip[j]) + + return min + +'''A recursive function to find the smallest distance. +The array Px contains all points sorted according to x coordinates and Py contains all points sorted according to y coordinates''' +def closestUtil(Px, Py, n): + if n <= 3: + return bruteForce(Px, n) + + mid = n // 2 + midPoint = Px[mid] + + Pyl = [None] * mid + Pyr = [None] * (n-mid) + li = ri = 0 + for i in range(n): + if ((Py[i].x < midPoint.x or (Py[i].x == midPoint.x and Py[i].y < midPoint.y)) and li