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 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
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.
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 an Arduino code that does the following. Generate 50 random numbers between the numbers 100...
Write an Arduino code that does the following. Generate 50 random numbers between the numbers 100 and 300. Pick a number at random out of these 50 random variables. a. Determine the probability of the chosen number being greater than 200. This may be achieved by counting the numbers that are greater than 200 and dividing the count by 50. Make sure you, i.Formulate the appropriate if-conditions to check for a number being greater than 200 ii. Use a for-loop...
Python: Using Jupyter Notebook 1. Write code to generate Fibonacci series. Fibonacci numbers – 1, 1,...
Python: Using Jupyter Notebook 1. Write code to generate Fibonacci series. Fibonacci numbers – 1, 1, 2, 3, 5, 8, … 2. Check if a number is an Armstrong number A positive integer is called an Armstrong number of order n if abcd... = a^n + b^n + c^n + d^n + ... In case of an Armstrong number of 3 digits, the sum of cubes of each digits is equal to the number itself. For example: 153 = 1*1*1...
Using Python Write a GUI program that converts a distance in Meters to the equivalent distance...
Using Python Write a GUI program that converts a distance in Meters to the equivalent distance in Feet. The user should be able to enter a distance in Meters, click a button, and then see the equivalent distance in feet. Use the following formula to make the conversion: Meters = Feet x 0.304 For example, 1 Meter is 3.28 Feet.
Write a python script to solve the 4-queens problem using. The code should allow for random...
Write a python script to solve the 4-queens problem using. The code should allow for random starting, and for placed starting. "The 4-Queens Problem[1] consists in placing four queens on a 4 x 4 chessboard so that no two queens can capture each other. That is, no two queens are allowed to be placed on the same row, the same column or the same diagonal." Display the iterations until the final solution Hill Climbing (your choice of variant)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT