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
If my directed graph is represented as its incidence matrix how do I apply topological sort on that graph? I think it can be done by finding null rows and removing them with their corresponding columns but this is not efficient.How can I do this more efficiently?
I assume this is homework. Try the following algorithm:
1) Identify all of the nodes with indegree 0 (no edge points into the node)
2) For each node from step 1, perform a depth-first-search walk starting from the node.
If the graph is a DAG (directed acyclic graph -- no directed cycles like A -> B, B -> C, C -> A), the order in which you see nodes is guaranteed to be a topological ordering.
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 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)
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.
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
A minimum spanning tree gives the cheapest way an undirected graph. But what is a minimum spanning forest? Is it defined for connected graphs or unconnected graphs?
Minimum spanning forest is a generalization of minimum spanning tree for unconnected graphs. For every component of the graph, take its MST and the resulting collection is a minimum spanning forest.