Exemplo n.º 1
0
 def setUp(self):
     g = Graph()
     g.addEdge(0, 1)
     g.addEdge(0, 2)
     g.addEdge(0, 3)
     g.addEdge(2, 0)
     g.addEdge(2, 1)
     g.addEdge(1, 3)
     self.graph = g
     self.start = g.getVertex(2)
     self.target = g.getVertex(3)
Exemplo n.º 2
0
def mst(matrix, s):
    for i in range(len(matrix)):
        matrix[i][i] = sys.maxint

    graph = Graph()
    for i in range(len(matrix)):
        for j in range(len(matrix)):
            graph.addEdge(i, j, matrix[i][j])
    prim(graph, graph.getVertex(s))
    sum = 0
    for each in graph:
        sum += each.getDistance()

    return sum
Exemplo n.º 3
0
def knightGraph(bdSize):
    ktGraph = Graph()
    for row in range(bdSize):
        for col in range(bdSize):
            nodeId = posToNodeId(row, col, bdSize)
            newPositions = genLegalMoves(row, col, bdSize)
            for e in newPositions:
                nid = posToNodeId(e[0], e[1], bdSize)
                ktGraph.addEdge(nodeId, nid)

    path = []
    limit = bdSize * bdSize
    currentVertex = ktGraph.getVertex(0)
    x = knightTour(0, path, currentVertex, limit)
    y = path
Exemplo n.º 4
0
def buildGraph():
    g = Graph()

    #g.addEdge('u','v', 2)
    #g.addEdge('u','x', 1)
    #g.addEdge('v','w', 3)
    #g.addEdge('w','z', -5)
    #g.addEdge('u','w', 5)
    #g.addEdge('v','x', 2)
    #g.addEdge('x','y', 1)
    #g.addEdge('y','z', 1)
    #g.addEdge('x','w', 3)
    #g.addEdge('y','w', 1)

    #Fig 4.14 from S Dasgupta
    g.addEdge('s', 'a', 10)
    g.addEdge('s', 'g', 8)
    g.addEdge('g', 'f', 1)
    g.addEdge('a', 'e', 2)
    g.addEdge('f', 'a', -4)
    g.addEdge('f', 'e', -1)
    g.addEdge('e', 'b', -4)
    g.addEdge('b', 'a', 1)
    g.addEdge('b', 'c', 1)
    g.addEdge('c', 'd', 3)
    g.addEdge('d', 'e', -1)

    pq = PriorityQueue()

    start = g.getVertex('s')
    start.setDistance(0)
    pq.buildHeap([(v.getDistance(), v) for v in g])
    while not pq.isEmpty():
        currentVert = pq.delMin()
        for nextVert in currentVert.getConnections():
            newDist = currentVert.getDistance() \
                    + currentVert.getWeight(nextVert)
            if newDist < nextVert.getDistance():
                nextVert.setDistance(newDist)
                nextVert.setPred(currentVert)
                pq.decreaseKey(nextVert, newDist)

    return g
Exemplo n.º 5
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
        for nextVert in currentVert.getConnections():
            newDist = currentVert.getDistance() \
                + currentVert.getWeight(nextVert)

            if newDist < nextVert.getDistance():
                nextVert.setDistance(newDist)
                nextVert.setPred(currentVert)
                pq.decreaseKey(nextVert, newDist)


g = Graph()
g.addEdge(1, 2, 1)
g.addEdge(1, 5, 11)
g.addEdge(2, 3, 1)
g.addEdge(3, 4, 1)
g.addEdge(4, 5, 1)

startVertex = g.getVertex(1)

dijkstra(g, startVertex)

endVertex = g.getVertex(5)
predVertex = endVertex

# print(predVertex.getPred())

while predVertex is not None:
    print(predVertex.getId())
    predVertex = predVertex.getPred()
Exemplo n.º 7
0
        if not done:
            path.pop()
            u.setColor('white')
    #搜索成功
    else:
        done = True
        #将路径上的点都打印出来
        for each in path:
            print(each.id)
    return done


'''建图'''
g = Graph()
for each in 'ABCDEF':
    g.addVertex(each)
g.addEdge('A', 'B', 1)
g.addEdge('A', 'D', 1)
g.addEdge('B', 'D', 1)
g.addEdge('B', 'C', 1)
g.addEdge('C', '', 1)
g.addEdge('D', 'E', 1)
g.addEdge('E', 'B', 1)
g.addEdge('E', 'F', 1)
g.addEdge('F', 'C', 1)

A = g.getVertex('A')

lst = []
print(knightTour(1, lst, A, 6))
Exemplo n.º 8
0
from pythonds.graphs import Graph

def Dfs(currentVertex):
    currentVertex.setColor("gray")
    for vertex in currentVertex.getConnections():
        if vertex.getColor()=="white":
            Dfs(vertex)
    currentVertex.setColor("black")
    print currentVertex.getId()

g=Graph()
g.addEdge(0,1)
g.addEdge(0,3)
g.addEdge(1,2)
g.addEdge(1,3)
g.addEdge(3,4)
g.addEdge(4,5)
g.addEdge(4,1)
g.addEdge(5,2)

Dfs(g.getVertex(0))

Exemplo n.º 9
0
def buildGraph(list):
    for word in list:
        for i in range(len(word)):
            bucket = word[:i] + "_" + word[i + 1:]
            if bucket in d:
                d[bucket].append(word)
            else:
                d[bucket] = [word]
    for bucket in d.keys():
        for word1 in d[bucket]:
            for word2 in d[bucket]:
                if (word1 != word2):
                    g.addEdge(word1, word2)


'''
buildGraph(["hit","hot","dot","dog","lot","log","cog"])
for v in g:
   for w in v.getConnections():
       print("( %s , %s )" % (v.getId(), w.getId()))

bfs(g,g.getVertex("hit"),"cog")
'''

buildGraph([
    "fool", "foul", "foil", "fail", "fall", "cool", "pool", "poll", "pall",
    "pole", "pope", "pale", "sale", "sage", "page"
])

bfs(g, g.getVertex("fool"), "sage")
Exemplo n.º 10
0
                counter = currentvert.getDistance() + 1
                nbr.setDistance(currentvert.getDistance() + 1)
                nbr.setPred(currentvert)
                vertQueue.append(nbr)
        currentvert.setColor('black')

    return counter


t = 1

m = []
file = open("monk.txt", "r")
for _ in range(1):

    g = Graph()
    nodes, edgelist = [int(i) for i in input().split()]

    edgedata = [input().split() for _ in range(edgelist)]
    print(edgedata[0][0])
    for i in edgedata:
        #print('Add edge between {} and {}'.format(int(i[0]), int(i[1])))
        g.addEdge(int(i[0]), int(i[1]))
        g.addEdge(int(i[1]), int(i[0]))
    ans1 = bfs(g, g.getVertex(int(edgedata[0][0])))
    m.append(ans1)
    nodes, edgelist, edgedata = None, None, None

for i in m:
    print(i)
Exemplo n.º 11
0
G=Graph()
G.addEdge("u","v",2)
G.addEdge("v","u",2)
G.addEdge("v","w",3)
G.addEdge("w","v",3)
G.addEdge("u","w",5)
G.addEdge("w","u",5)
G.addEdge("u","x",1)
G.addEdge("x","u",1)
G.addEdge("x","v",2)
G.addEdge("v","x",2)
G.addEdge("x","v",2)
G.addEdge("x","w",3)
G.addEdge("w","x",3)
'''
G.addEdge("x","y",1)
G.addEdge("y","x",1)
G.addEdge("w","y",1)
G.addEdge("y","w",1)
G.addEdge("y","z",1)
G.addEdge("z","y",1)
'''
G.addEdge("w","z",5)
G.addEdge("z","w",5)



Diskistra(G.getVertex("u"),G)

for v in G:
    print ("( %s , %d)" % (v.getId(),v.getDistance()))
Exemplo n.º 12
0
from pythonds.graphs import Graph


def Dfs(currentVertex):
    currentVertex.setColor("gray")
    for vertex in currentVertex.getConnections():
        if vertex.getColor() == "white":
            Dfs(vertex)
    currentVertex.setColor("black")
    print currentVertex.getId()


g = Graph()
g.addEdge(0, 1)
g.addEdge(0, 3)
g.addEdge(1, 2)
g.addEdge(1, 3)
g.addEdge(3, 4)
g.addEdge(4, 5)
g.addEdge(4, 1)
g.addEdge(5, 2)

Dfs(g.getVertex(0))
Exemplo n.º 13
0
class WordLadder:
    def __init__(self):
        self.dictionary = {}
        self.graph = Graph()
        self.wordContainer = {}
        self.fileName = 'dictionary.json'
        self.noChain = []
        self.frequency = {}

    def readDictionary(self, fileName):
        self.file = open(self.fileName)
        self.dictionary = json.loads(self.file.read())

    def isNoChain(self, word):
        for n in range(len(word)):
            bucket = word[:n] + '$' + word[n + 1:]
            if (len(self.wordContainer[bucket]) > 1):
                return False
        self.frequency.setdefault(0, 0)
        self.frequency[0] += 1
        self.noChain.append(word)
        return True

    def parseDictionary(self):
        for word in self.dictionary:
            for n in range(len(word)):
                bucket = word[:n] + '$' + word[n + 1:]

                if bucket in self.wordContainer:
                    self.wordContainer[bucket].append(word)
                else:
                    self.wordContainer[bucket] = [word]

    def wordToGraph(self):
        for bucket in self.wordContainer.keys():
            for word1 in self.wordContainer[bucket]:
                for word2 in self.wordContainer[bucket]:
                    if word1 != word2:
                        self.graph.addEdge(word1, word2)

    def graphSearch(self, wordStart, wordEnd):
        start = self.graph.getVertex(wordStart)
        start.setDistance(0)
        start.setPred(None)
        vrtxQueue = Queue()
        vrtxQueue.enqueue(start)
        print(wordStart + "->", end='')

        while vrtxQueue.size() > 0:
            ls = []
            current = vrtxQueue.dequeue()
            prev = None
            for neighbour in current.getConnections():
                if neighbour.getColor() == "white":
                    neighbour.setColor("gray")
                    neighbour.setDistance(current.getDistance() + 1)
                    neighbour.setPred(current)
                    print(neighbour.getId() + "->", end='')
                    ##if neighbour.getId() == wordEnd :
                    #    print("\n\n\t",end='');
                    vrtxQueue.enqueue(neighbour)
            current.setColor("black")

    def test(self, wordStart):
        if self.isNoChain(wordStart):
            return False
        start = self.graph.getVertex(wordStart)
        start.setDistance(0)
        start.setPred(None)
        vrtxQueue = Queue()
        vrtxQueue.enqueue(start)
        #print(wordStart,end='')
        d = defaultdict(list)

        while vrtxQueue.size() > 0:
            ls = []
            current = vrtxQueue.dequeue()

            #print(current.getId(),end='')
            prev = None
            for neighbour in current.getConnections():
                if neighbour.getColor() == "white":
                    neighbour.setColor("gray")
                    neighbour.setDistance(current.getDistance() + 1)
                    neighbour.setPred(current)
                    dist = neighbour.getDistance()
                    word = neighbour.getId()
                    d[dist].append(word)
                    #print("->"+neighbour.getId(),end='')
                    ls.append(neighbour)
                    ##if neighbour.getId() == wordEnd :
                    #    print("\n\n\t",end='');
                    #vrtxQueue.enqueue(neighbour)
            for i in range(len(ls)):
                vrtxQueue.enqueue(ls.pop(0))

            current.setColor("black")
        outfile = open('Chains.txt', 'a')
        print("WORD : " + wordStart, file=outfile)
        for length in d.keys():
            print(
                '----------------------- Length : {} -----------------------'.
                format(length),
                file=outfile)
            self.frequency.setdefault(length, 0)
            self.frequency[length] += len(d[length])

            for word in d[length]:
                vert = self.graph.getVertex(word)
                print(vert.getId(), end='', file=outfile)
                for count in range(length):
                    print(" -> " + vert.getPred().getId(),
                          end='',
                          file=outfile)
                    #print()
                    vert = vert.getPred()
                print(file=outfile)
            print(file=outfile)

    def traverse(self, word):
        vertY = self.graph.getVertex(word)
        vertX = vertY
        while (vertX.getPred()):
            print(vertX.getId() + "->", end='')
            vertX = vertX.getPred()
        print(vertX.getId())

    def writeNoChain(self):
        outfile = open("noChain.txt", 'w')

        for word in self.noChain:
            print(word, file=outfile, end=',')

    def frequencyDistribution(self):
        for key, value in self.frequency.items():
            print("{} -> {}".format(key, value))
Exemplo n.º 14
0

G = Graph()
G.addEdge("u", "v", 2)
G.addEdge("v", "u", 2)
G.addEdge("v", "w", 3)
G.addEdge("w", "v", 3)
G.addEdge("u", "w", 5)
G.addEdge("w", "u", 5)
G.addEdge("u", "x", 1)
G.addEdge("x", "u", 1)
G.addEdge("x", "v", 2)
G.addEdge("v", "x", 2)
G.addEdge("x", "v", 2)
G.addEdge("x", "w", 3)
G.addEdge("w", "x", 3)
'''
G.addEdge("x","y",1)
G.addEdge("y","x",1)
G.addEdge("w","y",1)
G.addEdge("y","w",1)
G.addEdge("y","z",1)
G.addEdge("z","y",1)
'''
G.addEdge("w", "z", 5)
G.addEdge("z", "w", 5)

Diskistra(G.getVertex("u"), G)

for v in G:
    print("( %s , %d)" % (v.getId(), v.getDistance()))
Exemplo n.º 15
0
                for word2 in d[bucket]:
                    if word1 != word2:
                        g.addEdge(word1, word2)
        return g

    def bfs(g, start):
        start.setDistance(0)
        start.setPred(None)
        vertQueue = Queue()
        vertQueue.enqueue(start)
        while (vertQueue.size() > 0):
            currentVert = vertQueue.dequeue()
            for nbr in currentVert.getConnections():
                if (nbr.getColor() == 'white'):
                    nbr.setColor('gray')
                    nbr.setDistance(currentVert.getDistance() + 1)
                    nbr.setPred(currentVert)
                    vertQueue.enqueue(nbr)
            currentVert.setColor('black')

    def traverse(y):
        x = y
        while (x.getPred()):
            print(x.getId())
            x = x.getPred()
        print(x.getId())


g = Graph()
print(g.getVertex('sage'))
#print(g.traverse(t))
Exemplo n.º 16
0
def bfs(g, start):
    start.setDistance(0)
    start.setPred(None)
    vertQueue = Queue()
    vertQueue.enqueue(start)
    while (vertQueue.size() > 0):
        currentVert = vertQueue.dequeue()
        for nbr in currentVert.getConnections():
            if (nbr.getColor() == 'white'):
                nbr.setColor('gray')
                nbr.setDistance(currentVert.getDistance() + 1)
                nbr.setPred(currentVert)
                vertQueue.enqueue(nbr)
        currentVert.setColor('black')
        print(currentVert)  # prints the status of the current visited vertex


def traverse(y):
    x = y
    while (x.getPred()):
        print(x.getId())
        x = x.getPred()
    print(x.getId())


g = Graph()
g = buildGraph('wordFile.txt')

bfs(g, g.getVertex('fool'))
Exemplo n.º 17
0
from bfs import bfs
d={}
g=Graph()
def buildGraph(list):
    for word in list:
        for i in range(len(word)):
            bucket=word[:i]+"_"+word[i+1:]
            if bucket in d:
                d[bucket].append(word)
            else: d[bucket]=[word]
    for bucket in d.keys():
        for word1 in d[bucket]:
            for word2 in d[bucket]:
                if (word1!=word2):
                    g.addEdge(word1,word2)


'''
buildGraph(["hit","hot","dot","dog","lot","log","cog"])
for v in g:
   for w in v.getConnections():
       print("( %s , %s )" % (v.getId(), w.getId()))

bfs(g,g.getVertex("hit"),"cog")
'''

buildGraph(["fool","foul","foil","fail","fall","cool","pool",
"poll","pall","pole","pope","pale","sale","sage","page"])

bfs(g,g.getVertex("fool"),"sage")
Exemplo n.º 18
0
                vertex.setPred(currentVertex)
                pq.decreaseKey(vertex,newCost)


G=Graph()
G.addEdge("a","b",2)
G.addEdge("b","a",2)
G.addEdge("a","c",3)
G.addEdge("c","a",3)
G.addEdge("b","c",1)
G.addEdge("c","b",1)
G.addEdge("b","d",1)
G.addEdge("d","b",1)
G.addEdge("b","e",4)
G.addEdge("e","b",4)
G.addEdge("d","e",1)
G.addEdge("e","d",1)
G.addEdge("c","f",5)
G.addEdge("f","c",5)
G.addEdge("e","f",1)
G.addEdge("f","e",1)
G.addEdge("f","g",1)
G.addEdge("g","f",1)



prim(G.getVertex("a"),G)
'''
for v in G:
    print ("( %s , %d)" % (v.getId(),v.getDistance()))
'''
Exemplo n.º 19
0
g.addEdge(2,5)
g.addEdge(2,6)
g.addEdge(5,6)
g.addEdge(6,7)
g.addEdge(7,6)
g.addEdge(3,7)
g.addEdge(8,7)
g.addEdge(4,8)
g.addEdge(8,4)

'''
wfile = open("SCC.txt",'r')

for line in wfile:
    str=line.strip()
    word=line[:-1].split()
    g.addEdge(word[0],word[1])


for vertex in g.getVertices():
    vert=g.getVertex(vertex)
    if(vert.getColor()!="black"):
        Dfs(vert)
reverseEdges()
connectedComponents()
print (maxLengths)
'''
for v in g1:
   for w in v.getConnections():
       print("( %s , %s )" % (v.getId(), w.getId()))
'''
Exemplo n.º 20
0
'''
def knightTour(n,path,u,limit):
        u.setColor('gray')
        path.append(u)
        if n < limit:
            nbrList = list(u.getConnections())
            i = 0
            done = False
            while i < len(nbrList) and not done:
                if nbrList[i].getColor() == 'white':
                    print u.getId()
                    done = knightTour(n+1, path, nbrList[i], limit)
                i = i + 1
            if not done:  # prepare to backtrack
                path.pop()
                print "backtrack at", u.getId()
                u.setColor('white')
        else:
            done = True
            print "yes done"
        return done



buildGraph(4)

path=[]
knightTour(1,path,g.getVertex(0),16)
print path,len(path)
print path
Exemplo n.º 21
0
            if newCost < vertex.getDistance():
                vertex.setDistance(newCost)
                vertex.setPred(currentVertex)
                pq.decreaseKey(vertex, newCost)


G = Graph()
G.addEdge("a", "b", 2)
G.addEdge("b", "a", 2)
G.addEdge("a", "c", 3)
G.addEdge("c", "a", 3)
G.addEdge("b", "c", 1)
G.addEdge("c", "b", 1)
G.addEdge("b", "d", 1)
G.addEdge("d", "b", 1)
G.addEdge("b", "e", 4)
G.addEdge("e", "b", 4)
G.addEdge("d", "e", 1)
G.addEdge("e", "d", 1)
G.addEdge("c", "f", 5)
G.addEdge("f", "c", 5)
G.addEdge("e", "f", 1)
G.addEdge("f", "e", 1)
G.addEdge("f", "g", 1)
G.addEdge("g", "f", 1)

prim(G.getVertex("a"), G)
'''
for v in G:
    print ("( %s , %d)" % (v.getId(),v.getDistance()))
'''
Exemplo n.º 22
0
            if (nbr.getColor() == "white"):
                nbr.setColor("gray")
                nbr.setDistance(currentVert.getDistance() + 1)
                nbr.setPred(currentVert)
                vertQueue.enqueue(nbr)

        currentVert.setColor("black")


def traverse(y):
    x = y
    while (x.getPred()):
        print(x.getId())
        x = x.getPred()
    print(x.getId())


g = Graph()
g.addEdge(0, 1, 5)
g.addEdge(0, 5, 2)
g.addEdge(1, 2, 4)
g.addEdge(2, 3, 9)
g.addEdge(3, 4, 7)
g.addEdge(3, 5, 3)
g.addEdge(4, 0, 1)
g.addEdge(5, 4, 8)
g.addEdge(5, 2, 1)

bfs(g, g.getVertex(0))
traverse(g.getVertex(4))
Exemplo n.º 23
0

def knightTour(n, path, u, limit):
    u.setColor('gray')
    path.append(u)
    if n < limit:
        nbrList = list(u.getConnections())
        i = 0
        done = False
        while i < len(nbrList) and not done:
            if nbrList[i].getColor() == 'white':
                print u.getId()
                done = knightTour(n + 1, path, nbrList[i], limit)
            i = i + 1
        if not done:  # prepare to backtrack
            path.pop()
            print "backtrack at", u.getId()
            u.setColor('white')
    else:
        done = True
        print "yes done"
    return done


buildGraph(4)

path = []
knightTour(1, path, g.getVertex(0), 16)
print path, len(path)
print path
Exemplo n.º 24
0
g.addEdge(4,3)
g.addEdge(5,1)
g.addEdge(2,5)
g.addEdge(2,6)
g.addEdge(5,6)
g.addEdge(6,7)
g.addEdge(7,6)
g.addEdge(3,7)
g.addEdge(8,7)
g.addEdge(4,8)
g.addEdge(8,4)

'''
wfile = open("SCC.txt",'r')

for line in wfile:
    word=line[:-1].split()
    g.addEdge(word[0],word[1])

'''
for vertex in g.getVertices():
    vert=g.getVertex(vertex)
    if(vert.getColor()!="black"):
        Dfs(vert)
reverseEdges()
connectedComponents()
print (maxLengths)
for v in g1:
   for w in v.getConnections():
       print("( %s , %s )" % (v.getId(), w.getId()))
Exemplo n.º 25
0
            newDist = currentVert.getDistance()+currentVert.getWeight(nextVert)
            if newDist < nextVert.getDistance():#如果新距离小于原来记录的距离
                nextVert.setDistance(newDist)#将近距离记录下来
                nextVert.setPred(currentVert)#将当前节点设为父节点
                pq.decreaseKey(nextVert,newDist)#因为距离修改了,所以调整堆顺序

aGraph = Graph()
aGraph.addEdge('u','v',2)
aGraph.addEdge('u','x',1)
aGraph.addEdge('u','w',5)
aGraph.addEdge('v','u',2)
aGraph.addEdge('v','x',2)
aGraph.addEdge('v','w',3)
aGraph.addEdge('w','v',3)
aGraph.addEdge('w','x',3)
aGraph.addEdge('w','u',5)
aGraph.addEdge('w','y',1)
aGraph.addEdge('x','u',1)
aGraph.addEdge('x','v',2)
aGraph.addEdge('x','w',3)
aGraph.addEdge('x','y',1)
aGraph.addEdge('y','x',1)
aGraph.addEdge('y','w',1)
aGraph.addEdge('y','z',1)
aGraph.addEdge('z','w',5)
aGraph.addEdge('z','y',1)

dijkstra(aGraph,aGraph.getVertex('u'))
vs = aGraph.vertices.values()
for each in vs:
    print(each.id +':'+ str(each.getDistance()))
Exemplo n.º 26
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")


g.addEdge("B", "A", 2)
g.addEdge("B", "C", 1)
g.addEdge("B", "D", 1)
g.addEdge("C", "A", 3)
g.addEdge("C", "B", 1)
g.addEdge("C", "F", 5)
g.addEdge("D", "B", 1)
g.addEdge("D", "E", 1)
g.addEdge("E", "B", 4)
g.addEdge("E", "D", 1)
g.addEdge("E", "F", 1)
g.addEdge("F", "C", 5)
g.addEdge("F", "E", 1)
g.addEdge("F", "G", 1)

ga = g.getVertex("A")
prim(g, ga)

gc = g.getVertex("C")

cur = gc
while cur is not None:
    print(cur.getId())
    cur = cur.getPred()

gg = g.getVertex("G")
print("gg")
cur = gg
while cur is not None:
    print(cur.getId())
    cur = cur.getPred()