Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Hello
class Node: def __init__(self, val): self.val = val self.neighbors = {} def __repr__(self): return str(self.val) def __str__(self): return str(self.val) + ": " + str([neighbor for neighbor in self.neighbors.keys()]) class Graph: def __init__(self): self.nodesList = [] self.nodesCount = 0 def addNode(self, val): newnode = Node(val) if newnode not in self.nodesList: self.nodesList.append(newnode) self.nodesCount += 1 return newnode def addEdge(self, source, destination, cost = 0): if source not in self.nodesList: self.nodesList.append(source) self.nodesCount += 1 if destination not in self.nodesList: self.nodesList.append(destination) self.nodesCount += 1 source.neighbors[destination] = 1 def size(self): return self.nodesCount def __str__(self): result = '' for node in self.nodesList: result += str(node) + "\n" return result graph = Graph() node1 = graph.addNode( 10 ) node2 = graph.addNode( 20 ) node3 = graph.addNode( 30 ) graph.addEdge(node1, node2) graph.addEdge(node1, node3) graph.addEdge(node2, node3) graph.addEdge(node3, node1) print(graph) print('Size = ' + str(graph.size()))
run
|
edit
|
history
|
help
0
ex1 ficha nº2
exercicio A
VB.NET: Functions aren't required to return a value
Códigos dum boi olhando para um palácio
Selected Shakespeare Sonnet for doy mod 150
Declaração de Variaveis
Reed Solomon error correction (success)
CAI Visual Basic - Ej Mundial
Black Jack
CAI Visual Basic - Ej DJ