Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
14 changes: 14 additions & 0 deletions PartialSort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include <algorithm>
#include <functional>
#include <array>
#include <iostream>

int main()
{
std::array<int, 10> 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 << " ";
}
}
121 changes: 121 additions & 0 deletions TopologicalSort.java
Original file line number Diff line number Diff line change
@@ -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<List<Integer>> adjList = null;

// Constructor
Graph(List<Edge> 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<Edge> 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);
}
}
116 changes: 116 additions & 0 deletions kruskals-algorithm.java
Original file line number Diff line number Diff line change
@@ -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<Integer, Integer> 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<Edge> KruskalAlgo(List<Edge> edges, int N)
{
// stores edges present in MST
List<Edge> 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<Edge> 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<Edge> e = KruskalAlgo(edges, N);

for (Edge edge: e) {
System.out.println(edge);
}
}
}
119 changes: 119 additions & 0 deletions kruskalsAlgorithm.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
#include <iostream>
#include <vector>
#include <unordered_map>
#include <algorithm>
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<int, int> 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<Edge> KruskalAlgo(vector<Edge> edges, int N)
{
// stores edges present in MST
vector<Edge> 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<Edge> 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<Edge> e = KruskalAlgo(edges, N);

for (Edge &edge: e)
cout << "(" << edge.src << ", " << edge.dest << ", "
<< edge.weight << ")" << endl;

return 0;
}