Run Code
|
Code Wall
|
Users
|
Misc
|
Feedback
|
About
|
Login
|
Theme
|
Privacy
Breadth-First Path Finding
Language:
Ada
Assembly
Bash
C#
C++ (gcc)
C++ (clang)
C++ (vc++)
C (gcc)
C (clang)
C (vc)
Client Side
Common Lisp
D
Elixir
Erlang
F#
Fortran
Go
Haskell
Java
Javascript
Kotlin
Lua
MySql
Node.js
Ocaml
Octave
Objective-C
Oracle
Pascal
Perl
Php
PostgreSQL
Prolog
Python
Python 3
R
Ruby
Scala
Scheme
Sql Server
Swift
Tcl
Visual Basic
Layout:
Vertical
Horizontal
""" Breadth-First Path Finding """ 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()))
[
+
]
Show input
Absolute running time: 0.15 sec, cpu time: 0.04 sec, memory peak: 8 Mb, absolute service time: 0.16 sec
fork mode
|
history
|
discussion