I am using HASKELL for graph games. I am willing to get a suitable method for reach ability from a node to a particular node in the graph apart from using bfs or trees etc.
As I asked for code in haskell for reach ability from one node to a particular node, it is necessary to tell you that I am totally new to haskell. I have been reading the tutorials and simple examples, but when it come to implementation then I am lost. My graph is a directed graph, and say I want to check whether I can reach from node v to node w in graph.
From Data.Graph:
reachable :: Graph -> Vertex -> [Vertex]
To search the Haskell API and libraries:
http://www.haskell.org/hoogle/
http://holumbus.fh-wedel.de/hayoo/hayoo.html
Try representing your graph as a matrix where a 1 represents an edge.
E.g.:
Node/Node A B C D
A 0 0 1 1
B 0 0 1 1
C 0 0 1 0
D 1 0 1 0
For directed graphs the order of the matrix indices matters, for undirected graphs they don't. The above being a directed graph where there is an edge from D->C but not from C->D.
There are several All pair shortest path algorithms in hand. For small graphs, wikipedia says:
Floyd-Warshall algorithm is an
elegant, quickly implementable O(n3)
algorithm (Assumes absence of
negatively-weighed cycles).
EDIT: Are you looking for a ready-made Haskell code?
Not entirely sure what your question is, in the context of Haskell.
Are you asking for readymade implementations of the required algorithms + data structures?
Looking for libraries for graphs in Haskell?
Either way, check http://hackage.haskell.org for graph-related packages:
http://hackage.haskell.org/package/fgl
http://hackage.haskell.org/package/graphviz
http://hackage.haskell.org/package/Graphalyze
http://hackage.haskell.org/package/GraphSCC
http://hackage.haskell.org/package/hgal
Related
I understand the target approach for both the methods where Optimal Substructure calculates the optimal solution based on an input n while Overlapping Subproblems targets all the solutions for the range of input say from 1 to n.
For a problem like the Rod Cutting Problem. In this case while finding the optimal cut, do we consider each cut hence it can be considered as Overlapping Subproblem and work bottom-up. Or do we consider the optimal cut for a given input n and work top-down.
Hence, while they do deal with the optimality in the end, what are the exact differences between the two approaches.
I tried referring to this Overlapping Subproblem, Optimal Substructure and this page as well.
On a side note as well, does this relate to the solving approaches of Tabulation(top-down) and Memoization(bottom-up)?
This thread makes a valid point but I'm hoping if it could be broken down easier.
To answer your main question: overlapping subproblems and optimal substructure are both different concepts/properties, a problem that has both these properties or conditions being met can be solved via Dynamic Programming. To understand the difference between them, you actually need to understand what each of these term means in regards to Dynamic Programming.
I understand the target approach for both the methods where Optimal Substructure calculates the optimal solution based on an input n while Overlapping Subproblems targets all the solutions for the range of input say from 1 to n.
This is a poorly worded statement. You need to familiarize yourself with the basics of Dynamic Programming. Hopefully following explanation will help you get started.
Let's start with defining what each of these terms, Optimal Substructure & Overlapping Subproblems, mean.
Optimal Substructure: If optimal solution to a problem, S, of size n can be calculated by JUST looking at optimal solution of a subproblem, s, with size < n and NOT ALL solutions to subproblem, AND it will also result in an optimal solution for problem S, then this problem S is considered to have optimal substructure.
Example (Shortest Path Problem): consider a undirected graph with vertices a,b,c,d,e and edges (a,b), (a,e), (b,c), (c,d), (d,a) & (e,b) then shortest path between a & c is a -- b -- c and this problem can be broken down into finding shortest path between a & b and then shortest path between b & c and this will give us a valid solution. Note that we have two ways of reaching b from a:
a -- b (Shortest path)
a -- e -- b
Longest Path Problem does not have optimal substructure. Longest path between a & d is a -- e -- b -- c -- d, but sum of longest paths between a & c (a -- e -- b -- c) and c & d (c -- b -- e -- a -- d) won't give us a valid (non-repeating vertices) longest path between a & d.
Overlapping Subproblems: If you look at this diagram from the link you shared:
You can see that subproblem fib(1) is 'overlapping' across multiple branches and thus fib(5) has overlapping subproblems (fib(1), fib(2), etc).
On a side note as well, does this relate to the solving approaches of Tabulation(top-down) and Memoization(bottom-up)?
This again is a poorly worded question. Top-down(recursive) and bottom-up(iterative) approaches are different ways of solving a DP problem using memoization. From the Wikipedia article of Memoization:
In computing, memoization or memoisation is an optimization technique used primarily to speed up computer programs by storing the results of expensive function calls and returning the cached result when the same inputs occur again.
For the given fibonacci example, if we store fib(1) in a table after it was encountered the first time, we don't need to recompute it again when we see it next time. We can reuse the stored result and hence saving us lot of computations.
When we implement an iterative solution, "table" is usually an array (or array of arrays) and when we implement a recursive solution, "table" is usually a dynamic data structure, a hashmap (dictionary).
You can further read this link for better understanding of these two approaches.
I want to find a solution for set of numerical equations and I wondering whether Alloy could be used for that.
I've found limited information on alloy that seem to suggest (to me, at least) that it could be done, but I've found no examples of similar problems.
It certainly isn't easy, so before investing time and some money in literature I'd like to know if this is doable or not.
Simplified example:
(1) a + b = c, (2) a > b, (3) a > 0, (4) b > 0, (5) c > 0
One solution would be
a = 2, b = 1, c = 3
Any insights on the usability of Alloy or better tools / solutions would be greatly appreciated.
Kind regards,
Paul.
Daniel Jackson discourage using Alloy for numeric problems. The reason is that Alloy uses a SAT solver and this does not scale well since it severely limits the range of available integers. By default Alloy uses 4 bits for an integer: -8..7. (This can be enlarged with the run command but will of course slow down finding an answer.) The mindset of not to use numbers also influenced the syntax, there are no nice operators for numbers. I.e. addition is 5.plus[6].
That said, your problem would look like:
pred f[a,b,c : Int] {
a.plus[b] = c
a > b
a > 0
b > 0
c > 0
}
run f for 4 int
The answer can be found in the evaluator or text view. The first answer I got was a=4, b=1, c=5.
Alloy was developed around 2010 and since then there are SMT solvers that work similar to SAT solvers but can handle numeric problems as well. Alloy could be made to use those solvers I think. Would be nice because the language is incredibly nice to work with, the lack of number is a real miss.
Update Added a constraint puzzle at https://github.com/AlloyTools/models/blob/master/puzzle/einstein/einstein-wikipedia.als
Alloy is specialized as a relational constraint solver. While it can do very simple linear programming, you might want to look at a specialized tool like MiniZinc instead.
If a tree has k arcs, how many nodes does it have?
Base case:
If k=0 => n=(k+1)=1
Inductive hypothesis: For every k, n=(k+1) is true
Proof:
Is it true for k=1?
k=n-1
1=n-1
1=(k+1)-1
k=1,so:
1=1+1-1
1=1
Proved?
Am I missing something?
You never want to use 0 or 1 as your base case for proof by induction
If k=0 => n=(k+1)=1
You can prove anything you want if you use 0 (or often 1) as your base case. The classic example of this being that all horses are the same color. This is your mistake.
In addition, you can make graphs with the same number of arcs have different numbers of nodes, which immediately shows that you are not going to be able to use proof by induction to solve your problem.
Good luck!
I'm looking for the right keywords/nomenclature for the following problem, since I cannot find anything on google to this topic:
I have a graph where each edge and each node is assigned to a certain class/color or whatever you call it. Now I want to find a path between a start and a goal node, having some constraints on the path. For example I'd like to have as less "blue" nodes on the path as possible, or max. 2 "red" edges, or a combination of those things. Of course there are also the usual edge costs, which have to be minimized in addition to the fixed path constraints.
How is this kind of problem called, or what do I have to search for?
Best regards
Mark
I do not think that a name for such a general problem exists. However, I'm pretty certain you can re-model your graph and solve this problem via a simple Dijkstra search:
Trying to avoid certain (type of) vertex: Say you have a vertex that is to be avoided, and that has k neighbors. Replace it by a K_k (i.e. a clique with k vertices), and connect each neighbor to one of the k new vertices. Then set the weight of all the clique-edges to something large. Now every path passing over the original vertex will have to pass through the clique and "pay the fee", i.e. it will be avoided, if possible
Trying to avoid certain edges: Just raise their edge weight accordingly
Then, run a simple Dijkstra search. If you have multiple classes that are to be avoided, you can even set the weights as to determine priorities for avoiding each of them..
Hope that helps,
Lukas
I want to write program in haskell which will find the maximum flow in a flow network. However, I don't know Haskell well, so I don't know what is the best way to do it.
My biggest problem is finding augmenting path.
In Java I would create array of booleans Visited (to know which Node is visited) and residual graph rGraph and using DFS I would find augmenting paths (if there were some).
But in Haskell...
That's my definition of type Graph.
type Node = Char
type Edge = (Node ,Node, Int)
type Graph = ([Node], [Edge])
So graph with 5 nodes and 6 edges I can represent this way
['a','b','c','d','e'], [('a','b',7),('b','c',4),('a','d',8),('c','d',9),('e','b',11),('d','e',3)]
I can try to do it "java style", but I think there is a better way.
What is the best way to find augmenting path in graph in haskell?