From f5f297e723c828d388030822c40dac8ce3bd8805 Mon Sep 17 00:00:00 2001 From: Ajay Prakash Nair <42165927+ajaynair710@users.noreply.github.com> Date: Wed, 11 Dec 2019 20:30:35 +0530 Subject: [PATCH 1/4] Create kruskalsAlgorithm.cpp --- kruskalsAlgorithm.cpp | 119 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 119 insertions(+) create mode 100644 kruskalsAlgorithm.cpp diff --git a/kruskalsAlgorithm.cpp b/kruskalsAlgorithm.cpp new file mode 100644 index 0000000..8a0e8af --- /dev/null +++ b/kruskalsAlgorithm.cpp @@ -0,0 +1,119 @@ +#include +#include +#include +#include +using namespace std; + +// Data structure to store graph edges +struct Edge { + int src, dest, weight; +}; + +// Class to represent a disjoint set +class DisjointSet +{ + unordered_map parent; +public: + // perform MakeSet operation + void makeSet(int N) + { + // create N disjoint sets (one for each vertex) + for (int i = 0; i < N; i++) + parent[i] = i; + } + + // Find the root of the set in which element k belongs + int Find(int k) + { + // if k is root + if (parent[k] == k) + return k; + + // recur for parent until we find root + return Find(parent[k]); + } + + // Perform Union of two subsets + void Union(int a, int b) + { + // find root of the sets in which elements + // x and y belongs + int x = Find(a); + int y = Find(b); + + parent[x] = y; + } +}; + +// construct MST using Kruskal's algorithm +vector KruskalAlgo(vector edges, int N) +{ + // stores edges present in MST + vector MST; + + // initialize DisjointSet class + DisjointSet ds; + + // create singleton set for each element of universe + ds.makeSet(N); + + // MST contains exactly V-1 edges + while (MST.size() != N - 1) + { + // consider next edge with minimum weight from the graph + Edge next_edge = edges.back(); + edges.pop_back(); + + // find root of the sets to which two endpoint + // vertices of next_edge belongs + int x = ds.Find(next_edge.src); + int y = ds.Find(next_edge.dest); + + // if both endpoints have different parents, they belong to + // different connected components and can be included in MST + if (x != y) + { + MST.push_back(next_edge); + ds.Union(x, y); + } + } + return MST; +} + +// Comparison object to be used to order the Edges +struct compare +{ + inline bool operator() (Edge const &a, Edge const &b) + { + return (a.weight > b.weight); + } +}; + +// main function +int main() +{ + // vector of graph edges as per above diagram. + vector edges = + { + // (u, v, w) tiplet represent undirected edge from + // vertex u to vertex v having weight w + { 0, 1, 7 }, { 1, 2, 8 }, { 0, 3, 5 }, { 1, 3, 9 }, + { 1, 4, 7 }, { 2, 4, 5 }, { 3, 4, 15 }, { 3, 5, 6 }, + { 4, 5, 8 }, { 4, 6, 9 }, { 5, 6, 11 } + }; + + // sort edges by increasing weight + sort(edges.begin(), edges.end(), compare()); + + // Number of vertices in the graph + int N = 7; + + // construct graph + vector e = KruskalAlgo(edges, N); + + for (Edge &edge: e) + cout << "(" << edge.src << ", " << edge.dest << ", " + << edge.weight << ")" << endl; + + return 0; +} From 7735fb6a49b081fad6537dd6a369e208e31aab75 Mon Sep 17 00:00:00 2001 From: Ajay Prakash Nair <42165927+ajaynair710@users.noreply.github.com> Date: Wed, 11 Dec 2019 20:55:24 +0530 Subject: [PATCH 2/4] Create kruskals-algorithm.java --- kruskals-algorithm.java | 116 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 116 insertions(+) create mode 100644 kruskals-algorithm.java diff --git a/kruskals-algorithm.java b/kruskals-algorithm.java new file mode 100644 index 0000000..84cd2ad --- /dev/null +++ b/kruskals-algorithm.java @@ -0,0 +1,116 @@ +import java.util.*; + +// Data structure to store graph edges +class Edge +{ + int src, dest, weight; + + public Edge(int src, int dest, int weight) { + this.src = src; + this.dest = dest; + this.weight = weight; + } + + @Override + public String toString() { + return "(" + src + ", " + dest + ", " + weight + ")"; + } +}; + +// class to represent a disjoint set +class DisjointSet +{ + Map parent = new HashMap<>(); + + // perform MakeSet operation + public void makeSet(int N) + { + // create N disjoint sets (one for each vertex) + for (int i = 0; i < N; i++) + parent.put(i, i); + } + + // Find the root of the set in which element k belongs + private int Find(int k) + { + // if k is root + if (parent.get(k) == k) + return k; + + // recur for parent until we find root + return Find(parent.get(k)); + } + + // Perform Union of two subsets + private void Union(int a, int b) + { + // find root of the sets in which elements + // x and y belongs + int x = Find(a); + int y = Find(b); + + parent.put(x, y); + } + + // construct MST using Kruskal's algorithm + public static List KruskalAlgo(List edges, int N) + { + // stores edges present in MST + List MST = new ArrayList(); + + // initialize DisjointSet class + // create singleton set for each element of universe + DisjointSet ds = new DisjointSet(); + ds.makeSet(N); + + int index = 0; + + // MST contains exactly V-1 edges + while (MST.size() != N - 1) + { + // consider next edge with minimum weight from the graph + Edge next_edge = edges.get(index++); + + // find root of the sets to which two endpoint + // vertices of next_edge belongs + int x = ds.Find(next_edge.src); + int y = ds.Find(next_edge.dest); + + // if both endpoints have different parents, they belong to + // different connected components and can be included in MST + if (x != y) + { + MST.add(next_edge); + ds.Union(x, y); + } + } + return MST; + } + + public static void main(String[] args) + { + // (u, v, w) tiplet represent undirected edge from + // vertex u to vertex v having weight w + List edges = Arrays.asList( + new Edge(0, 1, 7), new Edge(1, 2, 8), + new Edge(0, 3, 5), new Edge(1, 3, 9), + new Edge(1, 4, 7), new Edge(2, 4, 5), + new Edge(3, 4, 15), new Edge(3, 5, 6), + new Edge(4, 5, 8), new Edge(4, 6, 9), + new Edge(5, 6, 11) + ); + + // sort edges by increasing weight + Collections.sort(edges, (a, b) -> a.weight - b.weight); + + // Number of vertices in the graph + final int N = 7; + + // construct graph + List e = KruskalAlgo(edges, N); + + for (Edge edge: e) { + System.out.println(edge); + } + } +} From b0f988e48112cc5a58d70bc1905a5a31a7032a45 Mon Sep 17 00:00:00 2001 From: Ajay Prakash Nair <42165927+ajaynair710@users.noreply.github.com> Date: Wed, 11 Dec 2019 21:02:01 +0530 Subject: [PATCH 3/4] Create TopologicalSort.java --- TopologicalSort.java | 121 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 121 insertions(+) create mode 100644 TopologicalSort.java diff --git a/TopologicalSort.java b/TopologicalSort.java new file mode 100644 index 0000000..c487b62 --- /dev/null +++ b/TopologicalSort.java @@ -0,0 +1,121 @@ +import java.util.*; + +// Data structure to store graph edges +class Edge +{ + int source, dest; + + public Edge(int source, int dest) { + this.source = source; + this.dest = dest; + } +}; + +// Class to represent a graph object +class Graph +{ + // A List of Lists to represent an adjacency list + List> adjList = null; + + // Constructor + Graph(List edges, int N) + { + // allocate memory + adjList = new ArrayList<>(N); + for (int i = 0; i < N; i++) { + adjList.add(i, new ArrayList<>()); + } + + // add edges to the undirected graph + for (int i = 0; i < edges.size(); i++) + { + int src = edges.get(i).source; + int dest = edges.get(i).dest; + + // add an edge from source to destination + adjList.get(src).add(dest); + } + } +} + +class TopologicalSort +{ + // Perform DFS on graph and set departure time of all + // vertices of the graph + static int DFS(Graph graph, int v, boolean[] discovered, + int[] departure, int time) + { + // mark current node as discovered + discovered[v] = true; + + // set arrival time + time++; + + // do for every edge (v -> u) + for (int u : graph.adjList.get(v)) + { + // u is not discovered + if (!discovered[u]) { + time = DFS(graph, u, discovered, departure, time); + } + } + + // ready to backtrack + // set departure time of vertex v + departure[time] = v; + time++; + + return time; + } + + // performs Topological Sort on a given DAG + public static void doTopologicalSort(Graph graph, int N) + { + // departure[] stores the vertex number using departure time as index + int[] departure = new int[2*N]; + Arrays.fill(departure, -1); + + // Note if we had done the other way around i.e. fill the + // array with departure time by using vertex number + // as index, we would need to sort the array later + + // stores vertex is discovered or not + boolean[] discovered = new boolean[N]; + int time = 0; + + // perform DFS on all undiscovered vertices + for (int i = 0; i < N; i++) { + if (!discovered[i]) { + time = DFS(graph, i, discovered, departure, time); + } + } + + // Print the vertices in order of their decreasing + // departure time in DFS i.e. in topological order + for (int i = 2*N - 1; i >= 0; i--) { + if (departure[i] != -1) { + System.out.print(departure[i] + " "); + } + } + } + + // Topological Sort Algorithm for a DAG using DFS + public static void main(String[] args) + { + // List of graph edges as per above diagram + List edges = Arrays.asList( + new Edge(0, 6), new Edge(1, 2), new Edge(1, 4), + new Edge(1, 6), new Edge(3, 0), new Edge(3, 4), + new Edge(5, 1), new Edge(7, 0), new Edge(7, 1) + ); + + // Set number of vertices in the graph + final int N = 8; + + // create a graph from edges + Graph graph = new Graph(edges, N); + + // perform Topological Sort + doTopologicalSort(graph, N); + } +} From 5fec5487250f4fbe07b3db1eced53b1abb5f4fd8 Mon Sep 17 00:00:00 2001 From: Ajay Prakash Nair <42165927+ajaynair710@users.noreply.github.com> Date: Fri, 13 Dec 2019 21:25:47 +0530 Subject: [PATCH 4/4] Create PartialSort.cpp --- PartialSort.cpp | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 PartialSort.cpp diff --git a/PartialSort.cpp b/PartialSort.cpp new file mode 100644 index 0000000..a3de7cd --- /dev/null +++ b/PartialSort.cpp @@ -0,0 +1,14 @@ +#include +#include +#include +#include + +int main() +{ + std::array s{5, 7, 4, 2, 8, 6, 1, 9, 0, 3}; + + std::partial_sort(s.begin(), s.begin() + 3, s.end()); + for (int a : s) { + std::cout << a << " "; + } +}