So I'm a cs student, and we were asked to build a backtracking program in c (no loops only recursion) which gets an adjacency matrix of an undirected unweighed (no lifts) graph, and returns the number of perfect matching in that graph or zero otherwise.
I thought of using fkt algorithm which uses the pfaffian orientation, but so far I haven't figured out how to do so.
If you could be so very kind and maybe direct me to the right book or the right way to look at this question I'll be very grateful.
It's the first time I tried to backtrack and I think I'm missing some basic conceptions of how to implement such a thing.
FKT is for planar graphs only. If you want to implement it (and you almost certainly don't, since that would be a lousy homework; this is for other people who find this question), you'll need to planarity test the graph in such a way that you obtain an embedding, then implement the orientation algorithm described in these slides. (Sketch: find a spanning tree in the primal graph and orient those edges arbitrarily. The edges not in the spanning tree comprise a spanning tree in the dual graph. Visit the nodes of the latter tree in postorder; the parent edge of each node (= primal face) visited is the last incident edge whose orientation is undetermined, so orient it clockwise if the face currently has an even number of clockwise edges and counterclockwise otherwise.)
Related
Problem
I have a list of approximatly 200000 nodes that represent lat/lon position in a city and I have to compute the Minimum Spanning Tree. I know that I need to use Prim algorithm but first of all I need a connected graph. (We can assume that those nodes are in a Euclidian plan)
To build this connected graph I thought firstly to compute the complete graph but (205000*(205000-1)/2 is around 19 billions edges and I can't handle that.
Options
Then I came across to Delaunay triangulation: with the fact that if I build this "Delauney graph", it contains a sub graph that is the Minimum Spanning Tree according and I have a total of around 600000 edges according to Wikipedia [..]it has at most 3n-6 edges. So it may be a good starting point for a Minimum Spanning Tree algorithm.
Another options is to build an approximately connected graph but with that I will maybe miss important edges that will influence my Minimum Spanning Tree.
My question
Is Delaunay a reliable solution in this case? If so, is there any other reliable solution than delaunay triangulation to this problem ?
Further information: this problem has to be solved in C.
The Delaunay triangulation of a point set is always a superset of the EMST of these points. So it is absolutely "reliable"*. And recommended, as it has a size linear in the number of points and can be efficiently built.
*When there are cocircular point quadruples, neither the triangulation nor the EMST are uniquely defined, but this is usually harmless.
There's a big question here of what libraries you have access to and how much you trust yourself as a coder. (I'm assuming the fact that you're new on SO should not be taken as a measure of your overall experience as a programmer - if it is, well, RIP.)
If we assume you don't have access to Delaunay and can't implement it yourself, minimum spanning trees algorithms that pre-suppose a graph aren't necessarily off limits to you. You can have the complete graph conceptually but not actually. Kruskal's algorithm, for instance, assumes you have a sorted list of all edges in your graph; most of your edges will not be near the minimum, and you do not have to compare all n^2 to find the minimum.
You can find minimum edges quickly by estimations that give you a reduced set, then refinement. For instance, if you divide your graph into a 100*100 grid, for any point p in the graph, points in the same grid square as p are guaranteed to be closer than points three or more squares away. This gives a much smaller set of points you have to compare to safely know you've found the closest.
It still won't be easy, but that might be easier than Delaunay.
Just for fun I'm trying to create a non-flash version of http://www.jurjans.lv/stuff/net/FreeNet.htm. It's all pretty straightforward stuff, but I'm mentally stuck on how to generate the initial network.
I could do it square by square with lots of if/else logic checking neighboring squares, but frankly it seems very laborious and I wonder if there's a much much MUCH smarter way. Generate a mathematical graph or something similar and then translate that into the grid?
I'm not asking for someone to code it all for me - just point me in the right direction!
The completed circuit appears to be a spanning tree.
There is an easy way to generate random minimum spanning trees by:
assigning random weights from some distribution to the edges of an undirected graph, and then constructing the minimum spanning tree of the graph.
So to summarise:
Build a graph with a vertex at the centre of each square
Add edges between each vertex and its neighbours above/down/left/right
Assign random weights (e.g. uniform real from 0 to 1) to each edge
Construct minimum spanning tree e.g. with Prim or Kruskal
Convert graph into tiles
If there are certain shapes you want to forbid (e.g. a fully connected vertex) you may want an extra iteration to increase the weight for edges used in any tiles that are illegal, and then regenerate the spanning tree until you end up with a legal graph.
Give an example of a graph that has the following properties. (Note that you need to give a single graph as the answer.)
The graph does not contain a triangle (that is, a clique of 3 vertices) as a subgraph.
Graph needs at least 4 colors for a proper vertex coloring
[If you think that such a graph is not possible, then prove that statement.]
1.Tomorrow we have a final exam, and this question may on the exam paper.
2.I think it is impossible to draw such a graph. But how to prove? Thank u very much.
A web search finds https://en.wikipedia.org/wiki/Gr%C3%B6tzsch_graph
The Grötzsch graph is a member of an infinite sequence of triangle-free graphs, each the Mycielskian of the previous graph in the sequence, starting from the null graph; this sequence of graphs was used by Mycielski (1955) to show that there exist triangle-free graphs with arbitrarily large chromatic number. Therefore, the Grötzsch graph is sometimes also called the Mycielski graph or the Mycielski–Grötzsch graph. Unlike later graphs in this sequence, the Grötzsch graph is the smallest triangle-free graph with its chromatic number (Chvátal 1974).
I am trying to get a vertex cover for an "almost" tree with 50,000 vertices. The graph is generated as a tree with random edges added in making it "almost" a tree.
I used the approximation method where you marry two vertices, add them to the cover and remove them from the graph, then move on to another set of vertices. After that I tried to reduce the number of vertices by removing the vertices that have all of their neighbors inside the vertex cover.
My question is how would I make the vertex cover even smaller? I'm trying to go as low as I can.
Here's an idea, but I have no idea if it is an improvement in practice:
From https://en.wikipedia.org/wiki/Biconnected_component "Any connected graph decomposes into a tree of biconnected components called the block-cut tree of the graph." Furthermore, you can compute such a decomposition in linear time.
I suggest that when you marry and remove two vertices you do this only for two vertices within the same biconnected component. When you have run out of vertices to merge you will have a set of trees not connected with each other. The vertex cover problem on trees is tractable via dynamic programming: for each node compute the cost of the best answer if that node is added to the cover and if that node is not added to the cover. You can compute the answers for a node given the best answers for its children.
Another way - for all I know better - would be to compute the minimum spanning tree of the graph and to use dynamic programming to compute the best vertex cover for that tree, neglecting the links outside the tree, remove the covered links from the graph, and then continue by marrying vertices as before.
I think I prefer the minimum spanning tree one. In producing the minimum spanning tree you are deleting a small number of links. A tree with N nodes had N-1 links, so even if you don't get back the original tree you get back one with as many links as it. A vertex cover for the complete graph is also a vertex cover for the minimum spanning tree so if the correct answer for the full graph has V vertices there is an answer for the minimum spanning tree with at most V vertices. If there were k random edges added to the tree there are k edges (not necessarily the same) that need to be added to turn the minimum spanning tree into the full graph. You can certainly make sure these new edges are covered with at most k vertices. So if the optimum answer has V vertices you will obtain an answer with at most V+k vertices.
Here's an attempt at an exact answer which is tractable when only a small number of links are added, or when they don't change the inter-node distances very much.
Find a minimum spanning tree, and divide edges into "tree edges" and "added edges", where the tree edges form a minimum spanning tree, and the added edges were not chosen for this. They may not be the edges actually added during construction but that doesn't matter. All trees on N nodes have N-1 edges so we have the same number of added edges as were used during creation, even if not the same edges.
Now pretend you can peek at the answer in the back of the book just enough to see, for one vertex from each added edge, whether that vertex was part of the best vertex cover. If it was, you can remove that vertex and its links from the problem. If not, the other vertex must be so you can remove it and its links from the problem.
You now have to find a minimum vertex cover for a tree or a number of disconnected trees, and we know how to do this - see my other answer for a bit more handwaving.
If you can't peek at the back of the book for an answer, and there are k added edges, try all 2^k possible answers that might have been in the back of the book and find the best. If you are lucky then added link A is in a different subtree from added link B. In that case you can confine the two calculations needed for the two possibilities for added link A (or B) to the dynamic programming calculations for the relevant subtree so you have only doubled the work instead of quadrupled it. In general, if your k added edges are in k different subtrees that don't interfere with each other, the cost is multiplied by 2 instead of 2^k.
Minimum vertex cover is an NP complete algorithm, which means that you can not solve it in a reasonable time even for something like 100 vertices (not to mention 50k).
For a tree there is a polynomial time greedy algorithm which is based on DFS, but the fact that you have "random edges added" screws everything up and makes this algorithm useless.
Wikipedia has an article about approximation algorithm, claims that it reaches factor 2 and claims that no better algorithm is know, which makes it quit unlikely that you will find one.
Im looking at this pdf as im trying to build an MSSP(multiple source shortest path) but im lacking the knowledge how to built interdegitating trees. until now i created the Spanning tree therefore the plannar graph is created, but im stuck cause i have no idea how i will build its dual. Is there any specific algorithm/approach or any paper which could help me solve this? As i searched and could find nothing useful.
If you don't have one already, you need a combinatorial embedding. There are efficient algorithms to obtain one from the incidence structure, but natural sources of planar graphs typically have a natural embedding. There are many ways to represent the embedding. I used a permutation π mapping each half-edge to the next half-edge in counterclockwise order with the same head vertex. With each (non-isolated) vertex is associated a circularly linked list of incoming half-edges.
Let rev be the permutation that maps each half-edge to its other half, with opposite head and tail vertices. The embedding permutation for the dual graph is the composition of π followed by rev. It maps each half-edge to the next half-edge on the face in clockwise order (or counterclockwise on the infinite face, because you're looking at its back side). This will be clearer if you try some examples by hand.
After you compute shortest paths from the initial root (I used Dijkstra, and unless your MSSP implementation is much faster than mine, there isn't much relative improvement to be had by using an asymptotically faster algorithm), do a depth-first search where the edges that belong to the shortest-path tree are ignored. (Another alternative is to visit the half-edges of the interdigitating tree in Euler-tour order, but this approach seemed as though it would incur extra logarithmic-time dynamic-tree operations.)