Kruskal worst case time scenario [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 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.

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

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.

Implementing A* Search algorithm [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 6 years ago.
Improve this question
I'm a beginner trying to implement the A* search algorithm for practice and I'm wondering what is the best way of going about it. I have created a graph structure (adjacency matrix) and my plan was to apply the A* to an initial and goal vertex. Also creating the heuristic and improving it as I go along. Question is, can this even work? I have had a look at other implementations and they have done it using different data structures.
That depends on how you implemented the adjacency matrix.
One critical point of A* is finding a node's neighbors. If you implement the matrix as a simple dense bit field, where you have a 1 for adjacent nodes and a 0 for non-adjacent ones, then this search is quite inefficient because you have to check every node. Although inefficient, this does not prevent you from implementing A*.
If you have a more involved implementation of the adjacency matrix, e.g. as a sparse matrix, which allows you to query the neighbors directly, this will be better suited for A*.

Determine whether there exists at least one path between two nodes in constant time [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
In a directed graph, Is it possible to determine whether there exists at least one path between two predefined nodes in constant time? If I use adjacency matrix data structure will it be useful?
Please suggest me what I am missing, what I need to learn. If there is no standard algorithm can you explain some solution for me.
Well, without pre-processing it cannot be done in constant time, you are bounded by the shortest path between these nodes to find the shortest path, and if no such path exists - it might decay to the size of the graph.
If you allow pre-processing, you can construct Strongly Connected Components graph (let it be G'), lexicographically sort it, and add an indication of all pairs (v',u') if there is a path from v' to u' on G'.
On query time you can search for the v' that contains v, and u' that contains u. and check if there is a path from v' to u', the answer will be the same.
If you pre-process the graph with Kruskal's algorithm, then it's subsequently possible to determine whether two nodes are connected in constant time. The algorithm will generate one or more sets of connected nodes. Two nodes that are in the same set are connected by one or more paths. Nodes in disjoint sets are not connected.
If you aren't allowed to pre-process the graph, then the answer is "No".

Resources