Skip to content
This repository was archived by the owner on Apr 10, 2020. It is now read-only.
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ Checkout the [contribution guidelines](https://github.com/Artris/algorithms/blob
| Karatsuba Multiplication | [Lec 11](https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-006-introduction-to-algorithms-fall-2011/lecture-videos/lecture-11-integer-arithmetic-karatsuba-multiplication/) | |
| Breadth-First Search (BFS) | [Lec 13](https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-006-introduction-to-algorithms-fall-2011/lecture-videos/lecture-13-breadth-first-search-bfs/) | [graphs / BreadthFirstSearch.re](https://github.com/Artris/algorithms/blob/master/src/graphs/BreadthFirstSearch.re) |
| Depth-First Search (DFS) | [Lec 14](https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-006-introduction-to-algorithms-fall-2011/lecture-videos/lecture-14-depth-first-search-dfs-topological-sort/) | [graphs / DepthFirstSearch.re](https://github.com/Artris/algorithms/blob/master/src/graphs/DepthFirstSearch.re) |
| Topological Sort | [Lec 14](https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-006-introduction-to-algorithms-fall-2011/lecture-videos/lecture-14-depth-first-search-dfs-topological-sort/) | |
| Topological Sort | [Lec 14](https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-006-introduction-to-algorithms-fall-2011/lecture-videos/lecture-14-depth-first-search-dfs-topological-sort/) | [graphs / TopologicalSort.re](https://github.com/Artris/algorithms/blob/master/src/graphs/TopologicalSort.re) |
| Dijkstra | [Lec 16](https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-006-introduction-to-algorithms-fall-2011/lecture-videos/lecture-16-dijkstra/) | |
| Bellman-Ford | [Lec 17](https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-006-introduction-to-algorithms-fall-2011/lecture-videos/lecture-17-bellman-ford/) | |
57 changes: 57 additions & 0 deletions __tests__/topological_sort_test.re
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
open Jest;
open Expect;

describe("Topological Sort", () => {
open TopologicalSort;

test("single node", () => {
let adj_list = [{id: "A", children: []}];
let expected_sorting = ["A"];
let sorting = sort(adj_list);

expect(sorting) |> toEqual(expected_sorting);
});

test("simple tree", () => {

let adj_list = [
{id: "A", children: ["B"]},
{id: "B", children: ["C"]},
{id: "C", children: ["D"]},
{id: "D", children: []}
];

let expected_sorting = ["A", "B", "C", "D"];
let sorting = sort(adj_list);

expect(sorting) |> toEqual(expected_sorting);
});

test("star graph", () => {

let adj_list = [
{id: "A", children: []},
{id: "B", children: ["A"]},
{id: "C", children: ["A"]},
{id: "D", children: ["A"]}];

let expected_sorting = ["D", "C", "B", "A"];
let sorting = sort(adj_list);

expect(sorting) |> toEqual(expected_sorting);
});

test("forest", () => {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can we add a test case similar to lecture 14 page 5? And make sure to visit B first?


let adj_list = [
{id: "A", children: ["B"]},
{id: "B", children: []},
{id: "C", children: ["D"]},
{id: "D", children: []}];

let expected_sorting = ["C", "D", "A", "B"];
let sorting = sort(adj_list);

expect(sorting) |> toEqual(expected_sorting);
});
});
73 changes: 73 additions & 0 deletions src/graphs/TopologicalSort.re
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
type node = {
id: string,
children: list(string),
};

type directedGraph = list(node);

exception Invalid_node_id(string);

exception Graph_not_DAG;

let parseAdjList = adj_list => {
let adj_tbl = Hashtbl.create(List.length(adj_list));
let insert = ({id, children}) => {
Hashtbl.add(adj_tbl, id, children);
};

List.iter(insert, adj_list);
let validateChildren = node => {
List.iter(child_id => {
if (!Hashtbl.mem(adj_tbl, child_id)) {
raise(Invalid_node_id(child_id));
}
}, node.children);
};

List.iter(validateChildren, adj_list);
adj_tbl;
};

let rec visit = (~node_id, ~adj_tbl, ~visited, ~ordering, ~ancestors) => {
if (Hashtbl.mem(visited, node_id)) {
let pred = id => id == node_id;
switch (List.find(pred, ancestors)) {
| exception Not_found => ordering;
| _ => raise(Graph_not_DAG);
};
}
else {
let ancestors = List.append(ancestors, [node_id]);
let children = Hashtbl.find(adj_tbl, node_id);
let visitChild = (ordering, child_id) => {
visit(
Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't like this pattern here. Let's create a record type for the arguments to this function state. We can then create a new state by replacing only parts of the state like

state with node_id = child_id

~node_id=child_id,
~adj_tbl=adj_tbl,
~visited=visited,
~ordering=ordering,
~ancestors=ancestors);
};

Hashtbl.add(visited, node_id, node_id);
let list = List.fold_left(visitChild, ordering, children);
List.append(list, [node_id]);
};
};

let sort = adj_list => {
let num_nodes = List.length(adj_list);
let adj_tbl = parseAdjList(adj_list);
let visited = Hashtbl.create(num_nodes);

let traverse = (ordering, node) => {
visit(
~node_id=node.id,
~adj_tbl=adj_tbl,
~visited=visited,
~ordering=ordering,
~ancestors=[]);
};

let top_sorting = List.fold_left(traverse, [], adj_list);
Copy link
Collaborator

Choose a reason for hiding this comment

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

what do you mean by top_sorting?

List.rev(top_sorting);
};
8 changes: 8 additions & 0 deletions src/graphs/TopologicalSort.rei
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
type node = {
Copy link
Collaborator

Choose a reason for hiding this comment

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

here we can use a type alias for string, type id = string and then we get

type node = {
  id: id,
  children: list(id)
}

...

let sort: directedGraph => list(id);

id: string,
children: list(string),
};

type directedGraph = list(node);

let sort: directedGraph => list(string);