Question

In: Computer Science

Give an algorithm that constructs an undirected connected graph, which allows for labeling of the n...

Give an algorithm that constructs an undirected connected graph, which allows for labeling of the n vertices u - v <= k for every edge (u,v), where k is some constant. All nodes have a unique label.

Solutions

Expert Solution

If the given condition for each edge (u,v) is | u-v | <= k (This condition will not holds true if we have k = 6 and N = 5, then let say we have one vertex as N = 5, then we need at least a vertex of -1, which is not possible) since the graph is undirected.

Our idea is simple, we will connect every i = N, N-1, N-2,..... to j = N - k, N-k +1, N-k+2,.... correspondingly to each other. This iteration is possible when we have an even number of vertices. So, we will traverse till we have our condition true and it will become false only in case of the odd value of N. So for the odd value of N, we will connect the leftover ith value to any edge. Then we will join each ith edge with (i+1)th edge to make the graph connected.

N = no. of vertices

Edges = set of unique pairs of vertices that need to be connected.

connected = boolean array with values false, it indicates whether the ith vertex is connected to any edge or not.

end = N

start = N - k

if start is zero then start = 1 // We don't have any vertex with value <=0

end of if

for end till 1 and end > start do :

if end - start <= k then :

make_pair(end, start) and put this into Edges set

connected[end] = connected[start] = true

start = start + 1

else break loop

end of for loop

// Now till here, our all vertices are not connected. Just extract any edge and start connecting every unconnected vertex to its end.

// For example, the extracted edge is 1--9, and leftovers are [ 4, 7, 2 ], then we will connect as 1--5--4---7--2.

edge = Edge[1]

last_vertex = edge.first

for i = 1 till N do:

if connected[i] is false then:

connected[i] = true

  make_pair(last_vertex , i) and put this into Edges set

last_vertex = i // remember, we are creating a chain of edge

end of if

end of for

for each edge in Edge do:

make_pair(second vertex of current edge, first vertex of next edge) and put this into Edge set

end of for

  


Related Solutions

Give an algorithm to detect whether a given undirected graph contains a cycle. If the graph...
Give an algorithm to detect whether a given undirected graph contains a cycle. If the graph contains a cycle, then your algorithm should output one. (It should not output all cycles in the graph, just one of them.) The running time of your algorithm should be O(m + n) for a graph with n nodes and m edges.
Is it always possible to make an undirected graph with two connected components connected by adding...
Is it always possible to make an undirected graph with two connected components connected by adding a single edge? • Why or why not? (Proof or counter example) [if true, give a proof, if false, give counter example] • Does the same hold true for directed graphs (and strongly connected components rather than connected components)? (Proof or counter example) [if true, give a proof, if false, give counter example]
Construct a connected undirected graph with 25 or more nodes. - Draw your graph in a...
Construct a connected undirected graph with 25 or more nodes. - Draw your graph in a diagram. - Draw the adjacency matrix of your graph. - Draw the adjacency list of your graph. - Run the BFS algorithm on your graph and complete the chart discussed in class. Detailed steps, including the changes of node color, predecessor, etc., as discussed in class, are required.
Design a linear-time algorithm which, given an undirected graph G and a particular edge e in...
Design a linear-time algorithm which, given an undirected graph G and a particular edge e in it, determines whether G has a cycle containing e. Your algorithm should also return the length (number of edges) of the shortest cycle containing e, if one exists. Just give the algorithm, no proofs are necessary. Hint: you can use BFS to solve this.
Write an algorithm to determine if the directed graph is strongly connected or not
Write an algorithm to determine if the directed graph is strongly connected or not
Which graph search method will be better for finding a cycle in an undirected graph: a...
Which graph search method will be better for finding a cycle in an undirected graph: a BTS or a DFS? In detail, describe (create) an algorithm for finding a cycle in any undirected graph, explaining why the algorithm assures to find a cycle if there exists one.
Let G be a simple undirected graph with n vertices where n is an even number....
Let G be a simple undirected graph with n vertices where n is an even number. Prove that G contains a triangle if it has at least (n^2 / 4) + 1 edges using mathematical induction.
You are given an undirected graph G = ( V, E ) in which the edge...
You are given an undirected graph G = ( V, E ) in which the edge weights are highly restricted. In particular, each edge has a positive integer weight from 1 to W, where W is a constant (independent of the number of edges or vertices). Show that it is possible to compute the single-source shortest paths in such a graph in O(E+V) time.
give an example of a simple, undirected, weighted graph such that breadth-firstsearch outputs a search-tree that...
give an example of a simple, undirected, weighted graph such that breadth-firstsearch outputs a search-tree that is not a single source shortest path tree. Youranswer must (a) Specify the graphG= (V, E)by specifyingVandE. (b) Specify the weight functionw. (c) Specify an ordering of the vertices and the search-tree output by breadth-first search assuming the specified graph and ordering. (d) Specify a valid single-source shortest path treeT= (V, ET)by specifyingETand its root, the first vertex in your specified ordering. (e) Include...
Given an undirected graph G = (V,E), consisting of n vertices and m edges, with each...
Given an undirected graph G = (V,E), consisting of n vertices and m edges, with each edge labeled from the set {0,1}. Describe and analyze the worst-case time complexity of an efficient algorithm to find any cycle consisting of edges whose labels alternate 0,1.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT