Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 6 years ago.
Improve this question
I am starting to learn time complexity, and I looked in the examples for the time complexity for some simple sort.
I wanted to know how do we calculate average time complexity for a depth-first search in a graph with |V|=n and |E|=m,let the start node be 'u' and end node be 'v'.
The time complexity for DFS is O(n + m). We get this complexity considering the fact that we are visiting each node only once and in the case of a tree (no cycles) we are crossing all the edges once.
For example, if the start node is u, and the end node is v, we are thinking at the worst-case scenario when v will be the last visited node.
So we are starting to visit each the first neighbor's of u connected component, then the second neighbor's connected component, and so on until the last neighbor's connected component, where we find v. We have visited each node only once, and didn't crossed the same edge more than once.
it will be O(n+m) if the graph is given in the form of adjacency list but
if the graph is in the form of adjacency matrix then the complexity is O(n*n),
as we have to traverse through the whole row until we find an edge.
Related
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
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.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 7 years ago.
Improve this question
The problem here is that i need to find the algorithm with linear time and all i can think of is Kruksal algorithm or Prim's algorithm which helps but that is already O(|E|log|E|).
As a hint, think about what happens if you focus on the edges in the graph whose weights are less than or equal to b. What can you say about the MST in the graph if
the graph given by those edges is connected?
the graph given by those edges is not connected?
As a hint, think about what would happen if you were to run Kruskal's algorithm on the graph. You don't need to actually run Kruskal's algorithm - that's not fast enough - but do think about how it would run were you to run it.
I could think of an algorithm like that:
1. go through each edge e in the graph (O(E)):
if w(e) > b:
remove e from the graph
2. if e with removed edges is disconnected (can be done in O(E)):
return false
return true
this procedure should answer the question but I'm not 100% sure. Any comments and suggestions are welcomed.
Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
Improve this question
Prove that if G is an undirected connected graph, then each of its edges is either in the depth-first search tree or is a back edge.
Now, from intuition and in class lectures by Steven Skiena, I know that the above holds true, since it dives all the way down, and then throw a rope back to a previous vertex. I also know that DFS is great in finding cycles.
However, my problem here is that I don't know how to 'prove' that the edge is either a tree edge or a back edge.
Consider the 4 cases possible (theoretically). An edge can be:
A tree edge
A back edge
Both a tree edge and a back edge
Neither a tree edge or a back edge
To prove what is needed, you need to show that cases 3 and 4 cannot happen, i.e. lead to a contradiction.
Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
I am wring thesis about shortest path algorithms.
And i don't understand one thing...
I have made visualisation of dijkstras algorithm.
1) Is it correct ? Or am i doing something wrong?
2) How would look Bellman-Ford algorithm? As fas as i have looked for difference, i found "Bellman-ford: the basic idea is very similar to Dijkstra's, but instead of selecting the shortest distance neighbour edges, it select all the neighbour edges." But also dijkstra checks all vertexes and all edges, isnt it?
dijkstra assumes that the cost of paths is montonically increasing. that plus the ordered search (using the priority queue) mans that when you first reach a node, you have arrived via the shortest path.
this is not true with negative weights. if you use dijkstra with negative weights then you may find a later path is better than an earlier one (because a negative weight improved the path on a later step).
so in bellman-ford, when you arrive at a node you test to see if the new path is shorter. in contrast, with dijkstra, you can cull nodes
in some (most) cases dijkstra will not explore all complete paths. for example, if G linked only back to C then any path through G would be higher cost that any through C. bellman-ford would still consider all paths through G to F (dijkstra would never look at those because they are of higher cost that going through C). if it does not do this it can't guarantee finding negative loops.
here's an example: the above never calculates the path AGEF. E has already been marked as visited by the time you arrive from G.
I am also thinking the same
Dijkstra's algorithm solves the single-source shortest-path problem when all edges have non-negative weights. It is a greedy algorithm and similar to Prim's algorithm. Algorithm starts at the source vertex, s, it grows a tree, T, that ultimately spans all vertices reachable from S. Vertices are added to T in order of distance i.e., first S, then the vertex closest to S, then the next closest, and so on.