Squares.Design and analyse an algorithm that takes as input an undirected graph G(V,E) and determines whether graph contains a simple cycle (that is, a cycle which doesn’t intersect itself) of length four. Its running time should be at mostO(V3) time.

You may assume that the input graph is represented either as an adjacency matrix or with adjacency lists, whichever makes your algorithm simpler.

Short Answer

Expert verified

The algorithm covers a shortest distance from the vertex A to vertexD is13 and the graph will takeO|v3| running time.

Step by step solution

01

Define the concept of the algorithm used for undirected graph

Dijkstra algorithm is an application of single source shortest path.

Dijkstra’s algorithm also known as SPF algorithm and is an algorithm for finding the shortest paths between the vertices in a graph. It returns a search tree for all the paths the given node can take. An acyclic graph is a directed graph that has no cycles. Its operation is performed in the minheap.

02

Time complexity of Dijkstra algorithm.

Time complexity:

TC=V+VlogV+E+ElogVTC=VlogV+ElogVTC=OV+ElogV

When the given graph is complete graph then,

role="math" localid="1659328321979" E=V2O(V2+V2.V)O(V2+V3)O(V3)TC=OV3

For finding the shortest path adjacent list and minheap both may use.

The time complexity is

And, if the graph is acyclic than also the complexity is:OV3

03

Design the Algorithm.

Dijkstra algorithm apply on the graph for finding the single source shortest path.

A directed graph with positive edge lengths, and returns the length of the shortest cycle in the graph and the graph is acyclic, which take time at mostOV3

So, here the vertex A is the source vertex. now take a minheap as a data structure for evaluate single source shortest path between the source and the destination.

From A the distance of A is zero and take the distance of vertex A from each and every vertex is infinity.

Theinput graph is represented either as an adjacency matrix or with adjacency lists is used to stored the vertices of the graph.

Now take A as the first vertex and evaluate the weight towards each vertex.

And choose the next vertex from the vertices which have minimum weight and select that node as the second vertex.

Then again evaluate the distance of it from every vertex and as get the minimum weight of the node and consider it as the main node.

Through this the series of vertex are arises.

here the vertex A is the source vertex. now take a minheap as a data structure for evaluate single source shortest path between the source and the destination.

From A the distance of A is zero and take the distance of vertex A from each and every vertices is infinity.

Select every vertex one by one and put it into the min heap as a data structure one by one as shown in the figure.

Fig: Adjacency list of the given graph

Hence, the shortest distance from the vertex A to vertex D is 13 .

And the graph will take OV3time.

Unlock Step-by-Step Solutions & Ace Your Exams!

  • Full Textbook Solutions

    Get detailed explanations and key concepts

  • Unlimited Al creation

    Al flashcards, explanations, exams and more...

  • Ads-free access

    To over 500 millions flashcards

  • Money-back guarantee

    We refund you if you fail your exam.

Over 30 million students worldwide already upgrade their learning with Vaia!

One App. One Place for Learning.

All the tools & learning materials you need for study success - in one app.

Get started for free

Most popular questions from this chapter

There is a network of roads G=(V,E) connecting a set of cities . Each road in E has an associated length Ie. There is a proposal to add one new road to this network, and there is a list E' of pairs of cities between which the new road can be built. Each such potential road localid="1659075853079" e'E' has an associated length. As a designer for the public works department you are asked to determine the road localid="1659075866764" e'E'whose addition to the existing network G would result in the maximum decrease in the driving distance between two fixed cities s and t in the network. Give an efficient algorithm for solving this problem.

Section 4.5.2 describes a way of storing a complete binary tree of n nodes in an array indexed by 1, 2, K, n .

(a) Consider the node at position j of the array. Show that its parent is at position [j2]and its children are at 2 jand 2 j + 1 (if these numbers are n).

(b) What the corresponding indices when a complete d-ary tree is stored in an array?

Figure 4.16 shows pseudocode for a binary heap, modeled on an exposition by R.E. Tarjan. The heap is stored as an array , which is assumed to support two constant-time operations:

  • |h|, which returns the number of elements currently in the array;
  • h-1, which returns the position of an element within the array.

The latter can always be achieved by maintaining the values of h-1as an auxiliary array.

(c) Show that themakeheapprocedure takesO(n) time when called on a set of elements. What is the worst-case input? (Hint:Start by showing that the running time is at most 1=1nlog(ni)).

(d) What needs to be changed to adapt this pseudocode to d-ary heaps?

Generalized shortest-paths problem.In Internet routing, there are delays on lines but also, more significantly, delays at routers. This motivates a generalized shortest-paths problem.

Suppose that in addition to having edge lengths {Ie:eE} ,a graph also has vertex costs {cV:vV} . Now define the cost of a path to be the sum of its edge lengths, plusthe costs ofall vertices on the path (including the endpoints). Give an efficient algorithm for the followingproblem.

Input:A directed graph G={V,E} positive edge lengths Ie and positive vertex costs cv; a starting vertex sv.

Output:An array cost[.] such that for every vertex u,costu, is the least cost of any path from s to u (i.e., the cost of the cheapest path), under the defnition above.

Notice that cost[s]=c.

You are given a directed graph G(V,E)with (possibly negative) weighted edges, along with a specific node sVand a tree T=(V,E'),E'E. Give an algorithm that checks whether T is a shortest-path tree for G with starting point s . Your algorithm should run in linear time.

Professor F. Lake suggests the following algorithm for finding the shortest path from node to node t in a directed graph with some negative edges: add a large constant to each edge weight so that all the weights become positive, then run Dijkstra’s algorithm starting at node s , and return the shortest path found to node t .

Is this a valid method? Either prove that it works correctly, or give a counterexample.

See all solutions

Recommended explanations on Computer Science Textbooks

View all explanations

What do you think about this solution?

We value your feedback to improve our textbook solutions.

Study anywhere. Anytime. Across all devices.

Sign-up for free