Most efficient data structure for Prim-Jarnik's algorithm [closed] - data-structures

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
What is the most efficient data structure among these there:
Edge list
Adjacency list
Adjacency matrix
for executing Prim-Jarnik's algorithm and why?

By edge list I suppose you mean the list of all edges in a graph G ? Then that is the slowest of the three since you would need to traverse the entire list each time you are at a vertice u just to know which (u, v) pairs are in G. Adjacency matrix is somewhat faster than that, but still slow since you will need to traverse an entire row of the matrix to find the adjacent vertices and the respective edge weights. But if you have a dense graph, the adjacency matrix is just like an adjacency list. Adjacency list is the faster one supposing a not so dense graph such that traversing the list isn't more costly than directly accessing each column in the matrix row.
Said that, the key issue in Prim's algorithm is not actually this. To achieve its described computational complexity, you need to use a priority queue (and this is the part you should be concerned).

Related

Question regarding what graphing algorithim to use [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I have a school question that I'm not sure what to code with. Lets say you have an undirected and unweighted graph G, which is a city road network. The nodes, n are intersections and m edges as the roads. Among the n nodes, there are h amount of hospitals. The question wants us to find for each node n, the distance from each node to the nearest hospital. Would it be possible to do using BFS or would djikstra be a better choice?
In addition, we would also need to propose a new algorithim that would find K amount of nearest hospitals nearest to each node with K being user input. In this case, is bfs still possible or is djikstra the only solution? Thank you.
The difference between Dijkstra and BFS is that with Dijkstra the queue is sorted so that closer nodes appear first.
In your case every edge has equal length and so this order comes automatically.
Thus, the algorithms are equal in this case.
Breadth-first search can be viewed as a special-case of Dijkstra's
algorithm on unweighted graphs, where the priority queue degenerates
into a FIFO queue.
https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm

Kruskal worst case time scenario [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I wanna make a graph that force the kruskal algorithm to worst case.
So, lets say that we dont care for sorting the edges time or other operations, but we care only about how we make the edges so that the algorithm makes the most union operations when taking them.
Maybe something like this. Can you give me an example with more nodes, or the idea for how to make the graph?
My idea is, take any graph with more nodes and with less no of cycles.For ex, take a graph with 20 nodes and with one cycle of length 3. In this case, The no of edges we can avoid in MST is only of that contribute cycle. If the graph doesn't contain more cycles we would obviously need many union operations, since most of the vertices belong to different sets.
If you want to have as many union operations as possible, why don't you consider a tree as input instead of a general graph? Simply because in a tree, (trivially) every edge needs to be added to the result set, so with one having n nodes, you will have n − 1 unions.
The edge weights won't matter any more.

heuristic for a directed random graph with positive weight edges [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I want to implement the A* algorithm and I have read about the heuristic function and how it works and I understand that an underestimate is needed to obtain an optimal path . But what heuristic function is the most suited for a random directed graph ? What I have tried so far is is taking the smallest edge weight from a node to the goal. As clearly the distance from a current node to the goal is not smaller than the smallest edge from a current node to the next.
The Manhattan distance only works when you have a well-defined distance metric that you can apply to pairs of nodes, such as with points in a 2D plane. For a graph, there's no inherent way to get the distance between two nodes.
With the little information available to you from the problem definition, I don't think you will do much better than using the heuristic that assumes all unseen edges have weight equal to the smallest weight in the graph.
You could get a bit more advanced if you sorted all the edges by weight. Then, as you see edges with particular weights during A*, you can remove them from the sorted list. This will let you know a running value of what the smallest remaining edge weight could be.

Image Data Structure [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 11 years ago.
An image(square image) can be stored as a tree: A node is white if the image is white, is black if the image is black, and is mixed if it contains both. White and black nodes are leaves, whereas a mixed node will have exactly 4 children, representing the 4 quadrants in the image. Given 2 images (trees), find the image representing their intersection. (Intersection: B^B -> B, B^W -> W, W^W->W)
This is a Google Interview Question
Here's a simple way to do it: Traverse both trees at the same time, using the same ordering. Build up an output tree while you do that. Then:
If you see a mixed node in both trees, output a mixed node
If you see a mixed node in one tree, but a white node in the other, output a white node (and ignore the mixed node in the traversal)
If you see a mixed node in one tree, but a black node in the other, copy the mixed node and it's children to the output tree
If you see two white nodes, output a white node
If you see two black nodes, output a black node
This has the possibility to create a mixed node that's actually only got white children, so you probably want a compression step where you traverse the tree collapsing mixed nodes that only have white children.
Edit: I think you could avoid the compression step by letting your recursion know whether output black nodes were found below (and putting in a white leaf if the answer was no).
Since both are represented as a binary tree you have to traverse a tree structure starting from root and check whats the color of nodes in both trees at any time and store the result in another tree. If either are black or white you stop traversing further. Else if anyone of them is mixed traverse further in that node until you find both of them of single color.

Polynomial time algorithm for finding a Hamiltonian walk in a graph [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 11 years ago.
Is there a polynomial time algorithm for finding a Hamiltonian walk in a graph?
My algorithm is N factorial and is really slow.
In general, as the (decision version of the) Hamiltonian Path problem is NP-complete, you cannot hope to get a polynomial-time algorithm for finding Hamiltonian paths. You can slightly speed it up with the usual N! → N22N dynamic programming trick (compute hp[v][w][S] = "is there a path that has endpoints v and w and whose vertices are the subset S" for every subset S and every two vertices v and w in it using DP), but that's still exponential.
However, there are many special kinds of graphs for which Hamiltonian paths will always exist, and they can be found easily (see work of Posa, Dirac, Ore, etc.)
For instance, the following is true: If every vertex of the graph has degree at least n/2, then the graph has a Hamiltonian path. You can in fact find one in O(n2), or IIRC even O(n log n) if you do it more cleverly.
[Rough sketch: First, just connect all vertices in some "Hamiltonian" cycle, nevermind if the edges are actually in the graph. Now for every edge (v,w) of your cycle that is not actually in the graph, consider the rest of the cycle: v...w. As deg(v)+deg(w)>=n, there exist consecutive x,y in your list (in that order) such that w is a neighbour of x and v is a neighbour of y. [Proof: Consider {the set of all neighbours of w} and {the set of all successors in your list of neighbours of v}; they must intersect.] Now change your cycle [v...xy...wv] to [vy...wx...v] instead, it has at least one less invalid edge, so you'll need at most n iterations to get a true Hamiltonian cycle. More details here.]
BTW: if what you are looking for is just a walk that includes every edge once, it's called an Eulerian walk and for graphs that have it (number of vertices of odd degree is 0 or 2), one can quite easily be found in polynomial time (fast).
You just asked the million dollar question. Finding a Hamilton path is an NP-complete problem. Some NP-hard problems can be solved in polynomial time using dynamic programming, but (to my knowledge) this is not one of them.
It's NP complete. But if you do manage to find a good method, let me know and I'll show you how to get rich.
Finding a better algorithm for the shortest is unlikely, as it is NP hard. But there are some heuristics that you could try, and perhaps you might want to consult your lecture notes for those ;) .
For less complexity you could find a short(ish) walk using the greedy algorithm.
Hmmm.. this depends on what your definitions are. An hamiltonian path is certainly NP-complete. However, an hamiltonian walk which can visit edges and vertices more than once (yes it's still called hamiltonian so long as you add the walk bit at the end) can be calculated in O(p^2logp) or O(max(c^2plogp, |E|)) so long as your graph meets a certain condition which Dirac first conjectured and the Takamizawa proved. See Takamizawa (1980) "An algorithm for finding a short closed spanning walk in a graph".
Paul
Depending on just how the graphs you're working with are generated you might be able to get expected polynomial time against a random instance by doing greedy path extension and then a random edge swap when that gets stuck.
This works well against randomly generated relatively sparse graphs guaranteed to have a Hamiltonian walk.
My Query: Show that a search problem RHAM for finding a Hamiltonian cycle in the graph G is
self-reducible
A search problem R is self-reducible if it is Cook-reducible to a decision problem
SR={ x : R(x) ≠ ∅ }

Resources