Graph Questions

¡Supera tus tareas y exámenes ahora con Quizwiz!

Vertex relaxation

All of our implementations actually relax all the edges pointing from a given vertex.

DAG Algorithm to find the shortest path from a source s to a destination t in O(V+E) time

Following is complete algorithm for finding shortest distances. 1) Initialize dist[] = {INF, INF, ....} and dist[s] = 0 where s is the source vertex. 2) Create a toplogical order of all vertices. 3) Do following for every vertex u in topological order. ...........Do following for every adjacent vertex v of u ..................if (dist[v] > dist[u] + weight(u, v)) ...........................dist[v] = dist[u] + weight(u, v)

Given a directed graph where every edge has weight as either 1 or 2, find the shortest path from a given source vertex 's' to a given destination vertex 't'. Expected time complexity is O(V+E).

If we apply Dijkstra's shortest path algorithm, we can get a shortest path in O(E + VLogV) time. How to do it in O(V+E) time? The idea is to use BFS . One important observation about BFS is, the path used in BFS always has least number of edges between any two vertices. So if all edges are of same weight, we can use BFS to find the shortest path. For this problem, we can modify the graph and split all edges of weight 2 into two edges of weight 1 each. In the modified graph, we can use BFS to find the shortest path. How is this approach O(V+E)? In worst case, all edges are of weight 2 and we need to do O(E) operations to split all edges, so the time complexity becomes O(E) + O(V+E) which is O(V+E).

Does the shortest path change when weights of all edges are multiplied by 10?

If we multiply all edge weights by 10, the shortest path doesn't change. The reason is simple, weights of all paths from s to t get multiplied by same amount. The number of edges on a path doesn't matter. It is like changing unit of weights

Given a directed weighted graph. You are also given the shortest path from a source vertex 's' to a destination vertex 't'. If weight of every edge is increased by 10 units, does the shortest path remain same in the modified graph?

The shortest path may change. The reason is, there may be different number of edges in different paths from s to t. For example, let shortest path be of weight 15 and has 5 edges. Let there be another path with 2 edges and total weight 25. The weight of the shortest path is increased by 5*10 and becomes 15 + 50. Weight of the other path is increased by 2*10 and becomes 25 + 20. So the shortest path changes to the other path with weight as 45.

Given a directed acyclic weighted graph, how to find the shortest path from a source s to a destination t in O(V+E) time?

We initialize distances to all vertices as infinite and distance to source as 0, then we find a topological sorting of the graph. Topological Sorting of a graph represents a linear ordering of the graph. Once we have topological order (or linear representation), we one by one process all vertices in topological order. For every vertex being processed, we update distances of its adjacent using distance of current vertex.

Does Dijkstra's algorithm work with negative weights?

There are two shortest paths algorithms known as Dijkstra's algorithm, depending on whether a vertex can be enqueued on the priority queue more than once. When the weights are nonnegative, the two versions coincide (as no vertex will be enqueued more than once). If we modify DijkstraSP.java so that a vertex cannot be enqueued more than once (e.g., using a marked[] array to mark those vertices that have been relaxed), then the algorithm is guaranteed to run in E log V time but it may yield incorrect results when there are edges with negative weights.

Edge relaxation

To relax an edge v->w means to test whether the best known way from s to w is to go from s to v, then take the edge from v to w, and, if so, update our data structures.


Conjuntos de estudio relacionados

BA 1500 - Chapter Overviews (Final)

View Set

Ch. 9-14 Anthropology (ANTH1200)

View Set

Patho Ch 33 best set: Diabetes Mellitus and the Metabolic Syndrome: Hormonal Control of Nutrient Metabolism and Storage

View Set

International Marketing Chapter 6

View Set

Ch. 4 Building Confidence to Speak

View Set

Home Owners Course Pre-test (Clearpoint)

View Set

Pediatric Diagnosis (Final Material)

View Set

quizlet abeka history of the world quiz 30

View Set