Add more code.
This commit is contained in:
parent
3ab4f89da6
commit
97fd1e4bea
31
other/breadthsearch.py
Normal file
31
other/breadthsearch.py
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
graph = {}
|
||||||
|
graph["you"] = ["alice", "bob", "claire"]
|
||||||
|
graph["bob"] = ["anuj", "peggy"]
|
||||||
|
graph["alice"] = ["peggy"]
|
||||||
|
graph["claire"] = ["thom", "jonny"]
|
||||||
|
graph["anuj"] = []
|
||||||
|
graph["peggy"] = []
|
||||||
|
graph["thom"] = []
|
||||||
|
graph["jonny"] = []
|
||||||
|
|
||||||
|
def person_is_seller(name):
|
||||||
|
return name[-1] =='m'
|
||||||
|
|
||||||
|
def search(name):
|
||||||
|
search_queue = deque()
|
||||||
|
search_queue += graph[name]
|
||||||
|
searched = []
|
||||||
|
while search_queue:
|
||||||
|
person = search_queue.popleft()
|
||||||
|
if not person in searched:
|
||||||
|
if person_is_seller(person):
|
||||||
|
print person + " is a mango seller!"
|
||||||
|
return True
|
||||||
|
else:
|
||||||
|
search_queue += graph[person]
|
||||||
|
searched.append(person)
|
||||||
|
return False
|
||||||
|
|
||||||
|
from collections import deque
|
||||||
|
search("you")
|
||||||
|
|
61
other/dijkstra
Normal file
61
other/dijkstra
Normal file
@ -0,0 +1,61 @@
|
|||||||
|
graph = {}
|
||||||
|
infinity = float("inf")
|
||||||
|
graph["start"] = {}
|
||||||
|
graph["start"]["a"] = 6
|
||||||
|
graph["start"]["b"] = 2
|
||||||
|
graph["start"]["c"] = infinity
|
||||||
|
|
||||||
|
graph["a"] = {}
|
||||||
|
graph["a"]["c"] = 4
|
||||||
|
graph["b"] = {}
|
||||||
|
graph["b"]["a"] = 3
|
||||||
|
graph["b"]["fin"] = infinity
|
||||||
|
graph["b"]["c"] = 8
|
||||||
|
|
||||||
|
graph["c"] = {}
|
||||||
|
graph["c"]["fin"] = 6
|
||||||
|
|
||||||
|
graph["fin"] = {}
|
||||||
|
|
||||||
|
costs = {}
|
||||||
|
costs["a"] = 6
|
||||||
|
costs["b"] = 2
|
||||||
|
costs["c"] = infinity
|
||||||
|
costs["fin"] = infinity
|
||||||
|
|
||||||
|
parents = {}
|
||||||
|
parents["a"] = "start"
|
||||||
|
parents["b"] = "start"
|
||||||
|
parents["fin"] = "c"
|
||||||
|
parents["c"] = "a"
|
||||||
|
parents["c"] = "b"
|
||||||
|
|
||||||
|
processed = []
|
||||||
|
|
||||||
|
|
||||||
|
def find_lowest_cost_node(costs):
|
||||||
|
lowest_cost = float("inf")
|
||||||
|
lowest_cost_node = None
|
||||||
|
for node in costs:
|
||||||
|
cost = costs[node]
|
||||||
|
if cost < lowest_cost and node not in processed:
|
||||||
|
lowest_cost = cost
|
||||||
|
lowest_cost_node = node
|
||||||
|
return lowest_cost_node
|
||||||
|
|
||||||
|
|
||||||
|
node = find_lowest_cost_node(costs)
|
||||||
|
while node is not None:
|
||||||
|
cost = costs[node]
|
||||||
|
neighbors = graph[node]
|
||||||
|
for n in neighbors.keys():
|
||||||
|
new_cost = cost + neighbors[n]
|
||||||
|
if costs[n] > new_cost:
|
||||||
|
costs[n] = new_cost
|
||||||
|
parents[n] = node
|
||||||
|
processed.append(node)
|
||||||
|
node = find_lowest_cost_node(costs)
|
||||||
|
|
||||||
|
print("Cost from the start to each node:")
|
||||||
|
print(costs)
|
||||||
|
print(parents)
|
Loading…
x
Reference in New Issue
Block a user