2020/11/22 _7
Daa Sce and Algoihm, Fall 2019 Impeial College Bine School
:///U//D/07/_7.
2/17
2020/11/22 _7
pthon ok — submit
pthon ok -q function_name
-v
pthon ok -q function_name -v
–submit
pthon ok –submit
pthon ok -q recap -u
pthon ok
:///U//D/07/_7.
3/17
2020/11/22 _7
node edge
adjacenc li diciona e child
Digraph
adj_list = ’a’:’b’, ‘b’:’c’, ‘d’, ‘c’:’d’, ‘d’:set() # ‘d’ has no nodes
saring from i, so emp se.
cla Digah(object):
“””
Direced graph.
edges is a dicionar mapping each node o a se of is children nodes.
nm_edges is he oal nmber of edges.
“””
def __init__(self):
self.edges = dict()
self.num_edges = 0
# more code
Graph Digraph
:///U//D/07/_7.
4/17
2020/11/22 _7
cla Gah(Digraph):
“””
Undireced graph: o one-a edges for eer edge
“””
def add_edge(self, src, dest):
Digraph.add_edge(self, src, dest)
Digraph.add_edge(self, dest, src)
inheiance Graph(Digraph) Graph Digraph
ses07.p ses07_data_structures.p
ses07.p
ses07_data_structures Graph, Digraph, Queue, QueueNode
ses07.p g = Graph()
f e07_daa_ce i Graph, Digraph, Queue, QueueNode
i e07_daa_ce a d
:///U//D/07/_7.
5/17
2020/11/22 _7
ds g = ds.Graph()
ses07.p ses07_data_structures.p
Digraph Graph create_bfs_graph ses07.p
pthon ok -q create_bfs_graph
ss
Graph
add_edge
g = create_lecture_graph()
g.children_of(‘Helena’)
g.has_node(‘Donald’)
g.has_edge(‘Donald’, ‘Helena’)
print(g) # his ill call he __sr__ mehod
:///U//D/07/_7.
6/17
2020/11/22 _7
Queue
diciona ee
Queue
q = Queue()
q.enqueue()
q.dequeue() q.is_empt()
ses07_data_structures.p
q = Queue()
q.enqueue(‘Ralf’)
q.enqueue(‘Sophie’)
print(q)
q.enqueue(‘Lu’)
print(q)
first_one_out = q.dequeue()
print(‘The first in the queue was: ‘ + first_one_out + ‘, but we just popped the
m out.’)
print(q)
print(q.is_empt())
:///U//D/07/_7.
7/17
2020/11/22 _7
graph.children_of(v) v
bfs ses07.p pthon ok -q bfs
dists start
None John
bfs_track_path ses07.p pthon ok -q bfs_track_path
print_paths
dists, prev_nodes = bfs_track_path(g, ‘John’)
print_path(prev_nodes, ‘Donald’)
:///U//D/07/_7.
8/17
2020/11/22 _7
moviesg.tt movies.tt
read_movie_data
g = read_movie_data(“movies.tt”)
s = ‘Bacon, Kevin’
print_children(g, s)
dists, prev_nodes = bfs_track_path(g, s)
to_find = ‘Kidman, Nicole’
print_path(prev_nodes, to_find)
:///U//D/07/_7.
9/17
2020/11/22 _7
Graph
distances =
‘a’: ’b’: 4, ‘c’: 2,
‘b’: ’c’: 1, ‘d’: 1,
‘c’: ’d’: 3,
‘d’:
a b e lecture_graph ses07_weighted.p
pthon ok -q lecture_graph
ses07_weighted_graphs.p Digraph Graph
:///U//D/07/_7.
10/17
2020/11/22 _7
edge aing in node e’e iied ending in node e hae no iied e
dijkstra ses07_weighted.p
min_dist, min_edge
graph = gs.Digraph(lecture_graph())
:///U//D/07/_7.
11/17
2020/11/22 _7
prev_nodes
prev_nodes
pthon ok -q dijkstra
print_path
(Yo do no need o implemen hi pa.) a b
pthon ok –submit
pthon ok
:///U//D/07/_7.
12/17
2020/11/22 _7
ack
dfs ses07_etra.p
pthon ok -q dfs
:///U//D/07/_7.
13/17
2020/11/22 _7
network
ses07_weighted_etra.p network
tube_data.ls
i ek a
G = create_lecture_graph_network()
shortest_dists = n.single_source_dijkstra_path_length(G, ‘A’)
all_shortest_dists = n.all_pairs_dijkstra_path_length(G)
# Read London ndergrond daa.
i ada a d
tube_data = pd.read_ecel(‘tube_data.ls’, sheetname=’Runtime’)
tube_data.describe()
G=n.Graph()
f inde, item i tube_data.iterrows():
G.add_edge(item[‘Station from (A)’], item[‘Station to (B)’], weight=item[‘AM
peak (0700-1000) Running Time (Mins)’])
# Ge all edges ih eighs
all_edges = G.edges(data=Te)
:///U//D/07/_7.
14/17
2020/11/22 _7
all_shortest_dists = n.all_pairs_dijkstra_path_length(G)
all_shortest_paths = n.all_pairs_dijkstra_path(G)
all_shortest_dists_dict = dict(all_shortest_dists)
all_shortest_paths_dict = dict(all_shortest_paths)
all_shortest_dists_dict[‘SOUTH KENSINGTON’][‘LIVERPOOL STREET’]
all_shortest_paths_dict[‘SOUTH KENSINGTON’][‘LIVERPOOL STREET’]
osmn network
:///U//D/07/_7.
15/17
2020/11/22 _7
heapq delete
cen
:///U//D/07/_7.
16/17
2020/11/22 _7
ses07_weighted_etra.p heapq
heapif(Q) Q
heappop(Q) heappush(Q, element)
heapq doenoceaeaepaaedaace heap o
random graph ses07 weighted etra.p
nodes = [str(i) f i i range(1000)] # Creae nodes ha are named inegers
ma_edges = 100000 # Ma nmber of edges for random graph
# Creae random graph and ime he o algorihms
g = random_graph(nodes, ma_edges)
len(g.list_edges())
start = nodes[0]
%iei dijkstra_heap(g,start)
%iei dijkstra(g,start)
:///U//D/07/_7.
17/17