def PrintGraph(self,Graph):
   labels = {}
   plt.figure(figsize=(12,12))
   pox=nx.spring_layout(Graph)
   for node in Graph.nodes():
     labels[node]='$'+str(node)+'$'
   nx.draw_networkx(Graph,pox,labels)
   plt.show()
Exemple #2
0
    def __init__(self, Graph):
        self.num_of_nodes = Graph.number_of_nodes()
        self.G = Graph
        self.DependencyMatrix = []
        self.AllProbes = []
        self.probeno = 0
        self.Node = collections.namedtuple('Node', 'NodeNum, parents')

        distToEndnode = {}
        for n in range(0, self.num_of_nodes):
            distToEndnode[n] = nx.shortest_path_length(self.G, 0, n)
        self.Maxdepth = max(distToEndnode.itervalues())
        #print self.Maxdepth
        self.initialization()
  def __init__(self, Graph, StartNode):   
    self.num_of_nodes = Graph.number_of_nodes()    
    self.G = Graph    
    self.DependencyMatrix = []
    self.AllProbes = []   
    self.probeno = 0
    self.MaxdepthSubGraph = -1
    self.Node = collections.namedtuple('Node', 'NodeNum, parents')
    self.ProbeToClus = []
    self.SourceNode = StartNode

    distToNode = {}
    for n in range(0,self.num_of_nodes):
      distToNode[n] = nx.shortest_path_length(self.G,0,n)
    self.Maxdepth = max(distToNode.itervalues())

    self.initialization()
Exemple #4
0
 def __init__(self, Graph):
   self.num_of_nodes = Graph.number_of_nodes()    
   self.G = Graph    
   self.DependencyMatrix = []
   self.AllProbes = []   
   self.probeno = 0
   self.Node = collections.namedtuple('Node', 'NodeNum, parents')
   self.SourceNode = -1
   
   mindist = 999999
   for node in G.nodes():
     dist = sum(nx.shortest_path_length(G,node,n) for n in G.nodes())
     if mindist > dist:
       mindist = dist
       self.SourceNode = node
   #print "startnode=%d"%startnode    
   
   distToEndnode = {}
   for n in range(0,self.num_of_nodes):
     distToEndnode[n] = nx.shortest_path_length(self.G,self.SourceNode,n)
   self.Maxdepth = max(distToEndnode.itervalues())
   #print self.Maxdepth    
   
   self.initialization()    
 def setUp(self):
     self.graph = Graph()
class Graph_Test(unittest.TestCase):
    
    #Runs before each test
    def setUp(self):
        self.graph = Graph()
    
    def test_add_vertex(self):
        self.graph.add_vertex('A', {'B': 7, 'C': 8})
        self.assertEqual(self.graph.vertices, {'A': {'C': 8, 'B': 7}})
        
    def test_shortest_path(self, ):
        self.graph.add_vertex('A', {'B': 7, 'C': 8})
        self.graph.add_vertex('B', {'A': 7, 'F': 2})
        self.graph.add_vertex('C', {'A': 8, 'F': 6, 'G': 4})
        self.graph.add_vertex('D', {'F': 8})
        self.graph.add_vertex('E', {'H': 1})
        self.graph.add_vertex('F', {'B': 2, 'C': 6, 'D': 8, 'G': 9, 'H': 3})
        self.graph.add_vertex('G', {'C': 4, 'F': 9})
        self.graph.add_vertex('H', {'E': 1, 'F': 3})
        
        self.assertEqual(self.graph.shortest_path('A', 'H'), ['H', 'F', 'B'])
        self.assertEqual(self.graph.shortest_path('H', 'I'), {'A': 12, 'B': 5, 'C': 9, 'D': 11, 'E': 1, 'F': 3, 'G': 12, 'H': 0})
 def setUp(self):
     self.graph = Graph()
class Graph_Test(unittest.TestCase):

    # Runs before each test
    def setUp(self):
        self.graph = Graph()

    def test_add_vertex(self):
        self.graph.add_vertex("A", {"B": 7, "C": 8})
        self.assertEqual(self.graph.vertices, {"A": {"C": 8, "B": 7}})

    def test_shortest_path(self,):
        self.graph.add_vertex("A", {"B": 7, "C": 8})
        self.graph.add_vertex("B", {"A": 7, "F": 2})
        self.graph.add_vertex("C", {"A": 8, "F": 6, "G": 4})
        self.graph.add_vertex("D", {"F": 8})
        self.graph.add_vertex("E", {"H": 1})
        self.graph.add_vertex("F", {"B": 2, "C": 6, "D": 8, "G": 9, "H": 3})
        self.graph.add_vertex("G", {"C": 4, "F": 9})
        self.graph.add_vertex("H", {"E": 1, "F": 3})

        self.assertEqual(self.graph.shortest_path("A", "H"), ["H", "F", "B"])
        self.assertEqual(
            self.graph.shortest_path("H", "I"), {"A": 12, "B": 5, "C": 9, "D": 11, "E": 1, "F": 3, "G": 12, "H": 0}
        )
Exemple #9
0
def main():
    numNodes = 6
    numRange = 25

    g = Graph()
    # Setup up nodes
    for i in range(numNodes):
        n = Node()
        g.addNode(n)

    # Connect nodes
    g.connect(1, 6, 14)
    g.connect(1, 3, 9)
    g.connect(1, 2, 7)
    g.connect(2, 3, 10)
    g.connect(2, 4, 15)
    g.connect(3, 6, 2)
    g.connect(3, 4, 11)
    g.connect(4, 5, 6)
    g.connect(6, 5, 9)

    # Test shortest path using Dijkstra's:
    startID = 1
    endID = 5
    pathLen, pathNodes = shortestPath(g, startID, endID)

    print("The shortest path between nodes {} and {} is {}, by taking path {}"\
        .format(startID, endID, pathLen, pathNodes))