Skip to content

Implemented Topological Sort #401

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 25, 2017
Merged
Changes from 1 commit
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
Next Next commit
Implement Topological Sort
- Implemented topological_sort
- Added auxiliary function build_topological
- Updated call to topological_sort
  • Loading branch information
antmarakis authored Mar 22, 2017
commit 081520758ecb915ca111999d098a4cd26ae3c0b1
52 changes: 47 additions & 5 deletions csp.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@
class CSP(search.Problem):
"""This class describes finite-domain Constraint Satisfaction Problems.
A CSP is specified by the following inputs:
variables A list of variables; each is atomic (e.g. int or string).
variables A list of variables; each is atomic (e.g. int or string).
domains A dict of {var:[possible_value, ...]} entries.
neighbors A dict of {var:[var,...]} that for each variable lists
the other variables that participate in constraints.
constraints A function f(A, a, B, b) that returns true if neighbors
A, B satisfy the constraint when they have values A=a, B=b

In the textbook and in most mathematical definitions, the
constraints are specified as explicit pairs of allowable values,
but the formulation here is easier to express and more compact for
Expand All @@ -29,7 +30,7 @@ class CSP(search.Problem):
problem, that's all there is.

However, the class also supports data structures and methods that help you
solve CSPs by calling a search function on the CSP. Methods and slots are
solve CSPs by calling a search function on the CSP. Methods and slots are
as follows, where the argument 'a' represents an assignment, which is a
dict of {var:val} entries:
assign(var, val, a) Assign a[var] = val; do other bookkeeping
Expand Down Expand Up @@ -307,8 +308,12 @@ def tree_csp_solver(csp):
"""[Figure 6.11]"""
assignment = {}
root = csp.variables[0]
X, parent = topological_sort(csp.variables, root)
X, parent = topological_sort(csp, root)
for Xj in reversed(X):
if(Xj == root):
# THIS SHOULD BE REMOVED
continue

if not make_arc_consistent(parent[Xj], Xj, csp):
return None
for Xi in X:
Expand All @@ -318,8 +323,45 @@ def tree_csp_solver(csp):
return assignment


def topological_sort(xs, x):
raise NotImplementedError
def topological_sort(X, root):
"""Returns the topological sort of X starting from the root.

Input:
X is a list with the nodes of the graph
N is the dictionary with the neighbors of each node
root denotes the root of the graph.

Output:
stack is a list with the nodes topologically sorted
parents is a dictionary pointing to each node's parent

Other:
visited shows the state (visited - not visited) of nodes

"""
nodes = X.variables
neighbors = X.neighbors

visited = {}
for x in nodes:
visited[x] = False

stack = []
parents = {}

build_topological(root, None, neighbors, visited, stack, parents)
return stack, parents

def build_topological(node, parent, neighbors, visited, stack, parents):
"""Builds the topological sort and the parents of each node in the graph"""
visited[node] = True

for n in neighbors[node]:
if(not visited[n]):
build_topological(n, node, neighbors, visited, stack, parents)

parents[node] = parent
stack.insert(0,node)


def make_arc_consistent(Xj, Xk, csp):
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy