Question regarding what graphing algorithim to use [closed] - algorithm

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

Related

How to approach coding for node disjoint path [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 am trying to solve the problem of node/vertices disjoint paths in a directed graph and came to know about the idea of splitting nodes into in and out nodes respectively. I got the idea and how it works and all the related theorem's like menger theorem but still, I'm not sure how to code it in an efficient manner.
Which data structure should I use so that I can split the vertices and still manage to balance the time complexity? Is there any algorithm existing which tells how to approach the code.
Please help or suggest some appropriate link which may help me out.
Thanks
It's quite simple actually. Let's say you have graph as an edge list of pairs u v meaning theres an edge from u to v
If nodes are not integers already use a dictionary/hash/map to reduce them to integers in range 1..n where n is number of nodes.
Now we "split" all nodes, for each node i it will become 2 nodes i and i+n. where i is considered in-node and i+n out-node.
Now graph edges are modified, for every edge u --> v we instead store edge u+n --> v
Also we add edges from each nodes in-node to out-node, i.e. from node i to i+n
We can assign infinity capacities to all edges and capacities of 1 to edges that connect in-node to out-node
Now Node-disjoint paths from some node s to t can be found using any max-flow algorithm (Ford-Fulkerson, Edmonds-Karp, Dinic, etc)
pesudo-code for building residual network:
n = #nodes
for each node i in 1..n:
residual_graph.addEdge(i, i+n, capacity=1);
residual_graph.addEdge(i+n, i, capacity=0);
for each edge (u,v) in graph
residual_graph.addEdge(u+n, v, capacity=+Infinity);
residual_graph.addEdge(v, u+n, capacity=0);

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.

Best way to implement shortest path algorithm with directed graph [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
Lets assume i have a weighted undirected graph with edges and wanted to find the shortest path as well as all possible paths that i could follow from the startpoint to the endpoint with distances, what would be the best way to implement this? Breadth depth search and k paths algorithm seem to offer reasonable solutions, although im not sure which is best
Sorry, can't post this as comments...
If you need all possible paths, you can't do really better than "tree" traversal (BFS or DFS for instance). Note that you'll need to consider each node as many times as it can be reached from the start (the "tree" is much bigger than the original graph - even infinite if you have cycles in your graph, but let's assume you don't).
To get the smallest path, you could look for it in your list in the end; or preferably, you could use a Dijkstra-like order for your tree traversal, so the shortest path will be the first to come up.

Check if there exists an undirected graph, given the degrees of its vertices [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 8 years ago.
Improve this question
N-numbers , d1,d2,d3..dn are given.
How do we check if it is possible to construct a undirected graph with vertices v1,v2,v3,...vn with degress d1,d2,...dn respectively.
Graph should not contain multiple edges between the same pair of nodes, or "loop" edges
(where both end vertices are the same node).
Also, what is the running time of the algorithm ?
This is what Wikipedia calls the graph realization problem, solvable by the Havel--Hakimi algorithm. Start with a graph having n vertices, v1..vn, and 0 edges. Define the deficit of a vertex vk to be the difference between dk and the current degree of vk. Repeatedly choose the vertex vk with the largest deficit D and connect it to the D other vertices having the D largest deficits. If a vertex would have negative deficit, then the instance is unsolvable. Otherwise, we terminate with a solution. I'll leave the running time as an exercise.

Resources