Question

In: Computer Science

Write a python code to generate a random undirected weighted graph and calculate the shortest distance...

Write a python code to generate a random undirected weighted graph and calculate the shortest distance from node i to node j (shortest path algorithm) where i is not equal to j. Store the results in text file. Thank you!

Solutions

Expert Solution

Python Code:

import random as rand

#We will use floyd warshall algorithm to find shortest path from any i to j

def floydWarshall(graph):
  spg=[]
  v=len(graph)
  #deep copy of graph to spg(shortest path graph)
  for i in range(0,v):
    spg.append([])
    for j in range(0,v):
      spg[i].append(graph[i][j])
  
  #floyd warshall algorithm
  for k in range(0,v):
    for i in range(0,v):
      for j in range(0,v):
        spg[i][j]=min(spg[i][j],spg[i][k]+spg[k][j])
  #return resultant graph
  return spg

#Providing random no.of vertices 
V=rand.randint(2,15)
INF=99999

undirected_graph=[]
#Generating random graph in adjacency matrix
for i in range(0,V):
  undirected_graph.append([])
  for j in range(0,V):
    undirected_graph[i].append(INF)

for i in range(0,V):
  for j in range(i+1,V):
    e=rand.randint(0,1)
    if e==1:
      w=rand.randint(2,500)
      undirected_graph[i][j]=w
      undirected_graph[j][i]=w

spg=floydWarshall(undirected_graph)
file=open("graph_report.txt",'w')
file.write("Graph Report\n\n")
file.write("Undirected graph generated:\n\n")
for i in range(0,V):
  file.write(" \t\t"+str(i+1))
file.write("\n")
for i in range(0,V):
  file.write(str(i+1)+"\t\t")
  for j in range(0,V):
    if undirected_graph[i][j]==INF:
      file.write("INF\t\t")
    else:
      file.write(str(undirected_graph[i][j])+"\t\t")
  file.write("\n")
file.write("\nNote:INF Means no path\n")
file.write("     SPB means Shortest Path Between\n\n")
for i in range(0,V):
  for j in range(i+1,V):
    if spg[i][j]<INF:
      file.write("SPB "+str(i+1)+" and "+str(j+1)+" is "+str(spg[i][j])+"\n")
file.close()

Sample Output:


Related Solutions

Using C++: Create a function to search an undirected weighted graph and find the highest weighted...
Using C++: Create a function to search an undirected weighted graph and find the highest weighted edge between two specific values. This should include a class declaration and the definition for all required members that are needed to support the search. NO NEED to code those members. You can assume any other data structures such as stack, heap, or linked list is defined (so you can use their standard functions without declaring or defining them).
Write a python script to calculate the average length of the game shortest game , longest...
Write a python script to calculate the average length of the game shortest game , longest game and the overall average length of a snake and ladder game The game length is the number of throws of the die. We are asked to write a python script which calculate that together with the average
Write a python script to calculate the average length of the game, Shortest game, longest game...
Write a python script to calculate the average length of the game, Shortest game, longest game and overall length of the game we need a python script that calculates the average length of a snake and ladder game. ie the number of moves it takes a single player to win the game. Then you run the game several times and will compare the results to come up with the required data(length of the game and number of moves )
Write a program in java that detects if there is a cycle in an undirected graph...
Write a program in java that detects if there is a cycle in an undirected graph using DFS Two lines of input: 1. Two integers V for the number of vertices and E for the number of edges respectively. 2. List of integers that shows how the graph is connected. Ex input: 4 4 01020323 Output: Graph contains cycle Ex input: 5 4 01020314 Output: Graph doesn't contains cycle
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...
5. Suppose we are given both an undirected graph G with weighted edges and a minimum...
5. Suppose we are given both an undirected graph G with weighted edges and a minimum spanning tree T of G . (a) Describe an algorithm to update the minimum spanning tree when the weight of a single edge e is decreased. (b) Describe an algorithm to update the minimum spanning tree when the weight of a single edge e is increased. In both cases, the input to your algorithm is the edge e and its new weight; your algorithms...
Write the R code to generate five independent uniform random numbers and use that to generate...
Write the R code to generate five independent uniform random numbers and use that to generate 5 independent Bernoulli random variables with probability of success p = 0.4. please use RStudio. please do not use lab nor rbern for calculations.
Suppose you are given an undirected, connected, weighted graph G. You want to find the heaviest...
Suppose you are given an undirected, connected, weighted graph G. You want to find the heaviest weight set of edges that you can remove from G so that G is still connected. Propose an optimal greedy algorithm for this problem and prove that it is optimal. (Hint: use matroid theory!).
Let G(V, E,w) be a weighted undirected graph, where V is the set of vertices, E...
Let G(V, E,w) be a weighted undirected graph, where V is the set of vertices, E is the set of edges, and w : E → R + is the weight of the edges (R + is the set of real positive numbers). Suppose T(G) is the set of all minimum spanning trees of G and is non-empty. If we know that the weight function w is a injection, i.e., no two edges in G have the same weight, then:...
Write a simple Python program that will generate two random between 1 and 6 ( 1...
Write a simple Python program that will generate two random between 1 and 6 ( 1 and 6 are included). If the sum of the number is grader or equal to 10 programs will display “ YOU WON!!!”. if the sum is less than 10, it will display “YOU LOST”. After this massage program will ask users a question: “if you want to play again enter Y. if you want to exit enter ‘N’) If the user enters Y they...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT