Skip to content
This repository was archived by the owner on Apr 10, 2020. It is now read-only.

Conversation

@selfeki
Copy link
Member

@selfeki selfeki commented Aug 18, 2018

No description provided.

@selfeki selfeki requested a review from alireza-a August 18, 2018 01:25
test("forest", () => {

let adj_list = [
{id: "A", neighbours: ["B"]},
Copy link
Collaborator

Choose a reason for hiding this comment

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

In topological sort, we are dealing with directed graphs. What's a better word for neighbors?

Copy link
Member Author

Choose a reason for hiding this comment

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

children


type directedGraph = list(node);

exception Not_found(string);
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 think we need to redefine the Not_found exception since it's defined in the Pervasive module.

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm aware, I thought it would be more informative for the user if we provide the value of the node which wasn't found as well, which the Pervasives version of Not_found doesn't provide.

Copy link
Member Author

@selfeki selfeki Aug 28, 2018

Choose a reason for hiding this comment

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

What do you think, should I just use the Pervasives module exception?

neighbours: list(string),
};

type mark =
Copy link
Collaborator

Choose a reason for hiding this comment

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

can you explain how mark is used?

Copy link
Member Author

@selfeki selfeki Aug 28, 2018

Choose a reason for hiding this comment

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

So this is the alg I'm using from https://en.wikipedia.org/wiki/Topological_sorting:
image

mark stores the mark type for each node (temporary or permanant).

Copy link
Collaborator

Choose a reason for hiding this comment

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

right, the algorithm mentioned in the MIT 6006 uses finish times.
https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-006-introduction-to-algorithms-fall-2011/lecture-videos/MIT6_006F11_lec14.pdf
Let's keep it consistent with their solution. So someone can watch the lecture and look at our implementation and just get it

Copy link
Collaborator

Choose a reason for hiding this comment

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

@selfeki I think we need a way to unify all the graph algorithms in a module. I don't like it all scattered where every function is in its own module.

Copy link
Member Author

Choose a reason for hiding this comment

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

@alireza-a, "I think we need a way to unify all the graph algorithms in a module.". Could you elaborate a bit more? I'm a little confused.


let rec visit = (~node_id, ~adj_tbl, ~mark_tbl, ~list) => {
if (Hashtbl.mem(mark_tbl, node_id)) {
switch (Hashtbl.find(mark_tbl, node_id)) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

you can match the exception instead of nesting.

adj_tbl;
};

let rec visit = (~node_id, ~adj_tbl, ~mark_tbl, ~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 is list?

Copy link
Collaborator

Choose a reason for hiding this comment

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

maybe a more descriptive name helps.

adj_tbl;
};

let rec visit = (~node_id, ~adj_tbl, ~mark_tbl, ~list) => {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm a bit confused about what happens here. Can you add some comments explaining how visit works

Copy link
Member Author

Choose a reason for hiding this comment

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

Sure, I'll add comments

@alireza-a
Copy link
Collaborator

@selfeki what's happening with this PR?

@@ -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);

~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?

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

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?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants