Exemplo n.º 1
0
def main():
     #file opening
     alphabet = "abcdefghijklmnopqrstuvwxyz"
     #file parsing
     word = []
     for line in fileinput.input():
          x = line.strip()
          t = x.lower()
          word.append(t)
     fileinput.close()
     g = Graph()     
     for i in word:
          vertlist = g.getVertices()
          if i not in vertlist:
               g.addVertex(i)
          #vert0 = g.getVertex(i)
          fhalf = shalf = []
          tmp = []
          
          for idx,ch in enumerate(i):
               l = list(i)
               for a in alphabet:
                    l[idx] = a
                    final = "".join(l)
                    if final in word and final not in vertlist:
                         g.addVertex(final)
                         vertlist = g.getVertices()
                    if final in word and final in vertlist:
                         #vert1 = g.getVertex(final)
                         g.addEdge(i, final)                     
                         g.addEdge(final, i)
          go = g.getVertex(i)
          s = go.getConnections()
          print i, len(s)

     return 0
Exemplo n.º 2
0
def main(args):

    # TODO:  add proper TESTs to initialize Graph and Vertex with graph example from GeeksForGeeks!

    # print type(Graph)
    # print dir(Graph)

    # print type(Vertex)
    # print dir(Vertex)
    # help(Vertex.__init__)

    # print dir(PriorityQueue)

    vertex0 = Vertex(0)
    vertex0.setDistance(0)
    # print vertex0

    vertex1 = Vertex(1)
    # print vertex1

    vertex7 = Vertex(7)
    # print vertex7

    vertex2 = Vertex(2)
    # print vertex2

    graph1 = Graph()
    graph1.addVertex(vertex0)
    graph1.addVertex(vertex1)
    graph1.addVertex(vertex7)
    graph1.addVertex(vertex2)

    print 'Initialized Vertex Dump:'
    # for aVertex in graph1.getVertices():
    #     print aVertex

    print 'Initialized Edges Dump:'
    graph1.addEdge(vertex0, vertex1, 4)
    graph1.addEdge(vertex0, vertex7, 8)
    graph1.addEdge(vertex1, vertex2, 8)

    print 'Vertex Sequenced after Djikstra from starting point with key 0'
    try:
        dijkstra(graph1, vertex0)
        for aVertex in graph1.getVertices():
            print aVertex
    except Exception as ex:
        logging.exception("BURP!")
Exemplo n.º 3
0
from pythonds.basic import Queue

def pathExists(start,end):
    vertexQueue=Queue()
    vertexQueue.enqueue(start)
    while (vertexQueue.size()>0):
        item=vertexQueue.dequeue()
        if item.getId()==end.getId():
            print ("path exists")
            return True
        else:
            for vertex in item.getConnections():
                if vertex.getColor()=="white":
                    vertex.setColor("gray")
                    vertexQueue.enqueue(vertex)
    return False

g=Graph()
data=True
while data!=1000:
    data=input("enter tuple")
    if data!=1000:
        g.addEdge(data[0],data[1])
while(True):
    tuple=input("Please enter tuple u want to know the path")
    print pathExists(g.getVertex(tuple[0]),g.getVertex(tuple[1]))
    for vertex in g.getVertices():
        g.getVertex(vertex).setColor("white")


Exemplo n.º 4
0
def escapeMaze( g, idStart, idExit ):
    vertStart = g.getVertex( idStart )
    vertExit = g.getVertex( idExit )
    resetGraph( g )
    bfs( g, vertStart )
    print( traverse( vertExit ) )

if __name__ == "__main__":
    
    gg = Graph()
    verts = 'ABCDEFGHI'
    #verts = 'AEGDBCFHI' 
    for i in verts:
        gg.addVertex(i)
        
    print( gg.getVertices() )
    gg.addEdge('B','A')
    gg.addEdge('A','B')
    gg.addEdge('B','C')
    gg.addEdge('C','F')
    gg.addEdge('F','H')
    gg.addEdge('H','I')
    gg.addEdge('I','F')
    gg.addEdge('B','E')
    gg.addEdge('E','D')
    gg.addEdge('D','B')
    gg.addEdge('E','A')
    gg.addEdge('D','G')
    gg.addEdge('G','E')

    allPairsShortestPath( gg )