Beispiel #1
0
def generateRandomTSPInstance(V, e):
    M = gr.generateRandMatrix(V, e)
    # generate a random adjacency matrix with n vertices, and a probability to be connected to each other vertex set to p
    G = gr.generateRandomGraph(M,
                               np.shape(M)[0], 0, 0, 0)
    # generate the graph with the adjacency matrix M
    n = np.size(G.getAdjacencyMatrix()[0])
    SP, SP_cost = np.array(sp.shortest_path(G.getAdjacencyMatrix(), n, n))
    connected_adj = gr.fromSpToAdjacency(sp.connectMatrix(SP_cost))
    #print("Adjacency graph of the connected matrix is\n", connected_adj);
    for i in range(np.shape(connected_adj)[0]):
        G.getVertex(i).setAdjacents(connected_adj[i], G)
    diameter = np.matrix.max((sp.shortest_path(G.getAdjacencyMatrix(), n,
                                               n))[1]).astype(int)
    # calculate the diameter in order to set the min/max deadlines on G
    G.setAllDeadlines(diameter, (2 * diameter) + 1)
    return G, SP_cost
Beispiel #2
0
 def getDensity(self):
     tot = 0;
     n = np.size(self.getAdjacencyMatrix()[0]);
     SP, SP_cost = np.array(sp.shortest_path(self.getAdjacencyMatrix(),n,n));
     for i in range(n):
         for j in range(n):
             if SP_cost[i,j]==1 and i!=j:
                 tot += 1;
     return (tot)/(n*(n-1));
Beispiel #3
0
# testgraph.py
import graphreader
import shortestpath

import pprint

nodes, edges = graphreader.load_graph('testgraph1.txt')
assert shortestpath.shortest_path('O', 'T', nodes, edges) == ['O', 'A', 'B', 'D', 'T']

# example from "Algorithm Design and Applications" textbook pg. 402, 403
nodes, edges = graphreader.load_graph('airportgraph.txt')
pprint.pprint(shortestpath.find_shortest_paths('BWI', nodes, edges))
pprint.pprint(shortestpath.shortest_path('BWI', 'LAX', nodes, edges))

nodes, edges = graphreader.load_graph('campusgraph.txt')
pprint.pprint(shortestpath.shortest_path('RR20', 'RR12', nodes, edges))
Beispiel #4
0
def getDiameter(G):
    n = np.size(G.getAdjacencyMatrix()[0]);
    SP, SP_cost = np.array(sp.shortest_path(G.getAdjacencyMatrix(),n,n));
    return np.matrix.max(SP_cost).astype(int);   
Beispiel #5
0
def getShortestPath(G, i, j):
    n = np.size(G.getAdjacencyMatrix()[0]);
    SP, SP_cost = np.array(sp.shortest_path(G.getAdjacencyMatrix(),n,n));
    return int(SP_cost.item(i,j));   
Beispiel #6
0
    return adj;
    
"""
Little testing to see if the algorithms work as expected
"""               
verbose = False; # this variable controls whether the output is printed
if verbose: 
    M = generateRandMatrix(15, 0.15); # generate a random adjacency matrix with n vertices, and a probability to be connected to each other vertex set to p
    G = generateRandomGraph(M, np.shape(M)[0], 0.2, 0, 0); # generate the graph with the adjacency matrix M
    #print("\n Targets on G are:");
    #print([int(i) for i in G.getTargets()]);
    print("Adjacency matrix:");
    print(G.getAdjacencyMatrix());
        #obtain the shortest path matrix (through a classical sp algorithm)
    n = np.size(G.getAdjacencyMatrix()[0]);
    SP, SP_cost = np.array(sp.shortest_path(G.getAdjacencyMatrix(),n,n));
    
    #print("\n Shortest path matrix:");
    #print(SP);
    
    print("\n Shortest Path's cost Matrix:");
    print(SP_cost);
    
    print("Density of the graph is ", G.getDensity());    
    
    connected_adj = fromSpToAdjacency(sp.connectMatrix(SP_cost));    
    print("Adjacency graph of the connected matrix is\n", connected_adj);
    for i in range(np.shape(connected_adj)[0]):
        G.getVertex(i).setAdjacents(connected_adj[i]);
    
    increased_adj = increaseEdgeDensity(connected_adj, 0.35);
Beispiel #7
0
def getpath():
    path = shortestpath.shortest_path(request.form);
    return path