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?
Related
I am trying to make basic constructions like "get intersection of a line and circle", "connect two points", "create a circle" with CGAL. However, the choice of kernel seems to be a problem. When I use exact 2D circular kernel, I get Circular_arc_point_2 from intersections, and then I can't use this data type to create lines, circles; and converting to Point_2 seems to introduce error and then stores the approximate value as an exact number. Additionally this problem seems to be independent of the choice of kernel.
What is a proper way of those constructions? Exact and approximate number are all fine as long as the data type is consistent in these constructions.
In the worst case, if this is unresolvable, is there any other free library with this functionality?
The predefined Exact_circular_kernel_2 only uses rational number as its field type. To cover every constructible point, you should define a circular kernel that uses a FieldWithSqrt. With existing types and traits it is simple:
using L = CGAL::Exact_predicates_exact_constructions_kernel_with_sqrt;
using A = CGAL::Algebraic_kernel_for_circles_2_2<L::FT>;
using K = CGAL::Circular_kernel_2<L, A>;
Then you can convert a Circular_arc_point_2 p to Point_2 with the exact coordinates:
K::Point_2 q(p.x(), p.y());
Circular_arc_point_2 is a point which coordinates are algebraic numbers of degree 2 (only way to represent exactly the intersection of 2 circles). You can convert the point into regular floating point coordinate Point_2 by using for example Point_2(to_double(cp.x()), to_double(cp.y())) but then you'll be loosing the exactness.
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'm having trouble using Haskell's type system elegantly. I'm sure my problem is a common one, but I don't know how to describe it except in terms specific to my program.
The concepts I'm trying to represent are:
datapoints, each of which takes one of several forms, e.g. (id, number of cases, number of controls), (id, number of cases, population)
sets of datapoints and aggregate information: (set of id's, total cases, total controls), with functions for adding / removing points (so for each variety of point, there's a corresponding variety of set)
I could have a class of point types and define each variety of point as its own type. Alternatively, I could have one point type and a different data constructor for each variety. Similarly for the sets of points.
I have at least one concern with each approach:
With type classes: Avoiding function name collision will be annoying. For example, both types of points could use a function to extract "number of cases", but the type class can't require this function because some other point type might not have cases.
Without type classes: I'd rather not export the data constructors from, say, the Point module (providing other, safer functions to create a new value). Without the data constructors, I won't be able to determine of which variety a given Point value is.
What design might help minimize these (and other) problems?
To expand a bit on sclv's answer, there is an extended family of closely-related concepts that amount to providing some means of deconstructing a value: Catamorphisms, which are generalized folds; Church-encoding, which represents data by its operations, and is often equivalent to partially applying a catamorphism to the value it deconstructs; CPS transforms, where a Church encoding resembles a reified pattern match that takes separate continuations for each case; representing data as a collection of operations that use it, usually known as object-oriented programming; and so on.
In your case, what you seem to want is an an abstract type, i.e. one that doesn't export its internal representation, but not a completely sealed one, i.e. that leaves the representation open to functions in the module that defines it. This is the same pattern followed by things like Data.Map.Map. You probably don't want to go the type class route, since it sounds like you need to work with a variety of data points, rather than on an arbitrary choice of a single type of data point.
Most likely, some combination of "smart constructors" to create values, and a variety of deconstruction functions (as described above) exported from the module is the best starting point. Going from there, I expect most of the remaining details should have an obvious approach to take next.
With the latter solution (no type classes), you can export a catamorphism on the type rather than the constructors..
data MyData = PointData Double Double | ControlData Double Double Double | SomeOtherData String Double
foldMyData pf cf sf d = case d of
(PointData x y) -> pf x y
(ControlData x y z) -> cf x y z
(SomeOtherData s x) -> sf s x
That way you have a way to pull your data apart into whatever you want (including just ignoring the values and passing functions that return what type of constructor you used) without providing a general way to construct your data.
I find the type-classes-based approach better as long as you are not going to mix different data points in a single data structure.
The name collision problem you mentioned can be solved by creating a separate type class for each distinct field, like this:
class WithCases p where
cases :: p -> NumberOfCases
How would one define a type for dimensions?
Can you define a type in terms of another type? (i.e. an inch is 72 PostScript points).
Would it even make sense to make a new type for a dimension unit?
I've seen libraries for other kind of units, but the dimensions I'd be interested in are:
scaled point (smallest, maybe Int?), point (65536 scaled points), pica (12 points), etc.
I think this is where phantom types can help. The dimensional package is a good place to start to understand them. The code is literate Haskell and very readable so I'd recommend reading through that.
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