Concern about Depth First Search [closed] - algorithm

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.

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

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.

A linear-time algorithm for checking whether a spanning tree can use edges of weight at most some number? [closed]

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.

A graph with n nodes and n vertices can contain just one cycle [closed]

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
If a graph unweighted G have the same number of Nodes and Edges , it is correct to assume that the graph G just contain one cycle ? Can be proved ?
EDIT: And all node are conected
If and only if there are one component in a graph. In other words, if from each node, there is a path to any node in a graph, you can assume that there are exactly one cycle.
You are assuming that all the nodes in the graph are connected. If the nodes in the graph are not connected, then answer to your question is no.
Also graph has to be a simple graph(https://en.wikipedia.org/wiki/Graph_(mathematics)?oldformat=true#Simple_graph)

Time complexity of depth-first graph algorithm [closed]

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.

Resources