Skip to content

Commit 5cbbc5a

Browse files
committedJul 27, 2018
Answers updated with the few steps that helped.
1 parent a4dd1bf commit 5cbbc5a

File tree

7 files changed

+158
-20
lines changed

7 files changed

+158
-20
lines changed
 

‎.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*.pyc
2+
__pycache__

‎Pipfile

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[[source]]
2+
url = "https://pypi.org/simple"
3+
verify_ssl = true
4+
name = "pypi"
5+
6+
[dev-packages]
7+
8+
[packages]
9+
10+
[requires]
11+
python_version = "2.7"

‎Pipfile.lock

+20
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎graph_dfs_debug/Answers.md

+15
Original file line numberDiff line numberDiff line change
@@ -1 +1,16 @@
11
Describe the fixes/improvements you made to the Graph implementation here.
2+
## Steps taken
3+
1. Run graph_demo
4+
2. Notice that there are no edges in this graph.
5+
3. Put error messages in, found that we're not passing some key variables
6+
to the functions in `graph_demo`.
7+
4. Add some error messages to the `add_vertex` function in `graph` file so
8+
we know for sure that we're adding the right thing.
9+
5. Mod `add_edge` function - add end to start and vice versa.
10+
6. Run `graph_demo`, still getting errors.
11+
7. Replaced search functions with working code from lecture - bfs/dfs
12+
returns `visited`, which our `find_components` function is looking for.
13+
8. Add more error messages and print statements to `main` function of
14+
`graph_demo` - apparently we're not passing 4 args. Our primary variables are
15+
not defined.
16+
9.

‎graph_dfs_debug/graph.html

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
2+
<!DOCTYPE html>
3+
<html lang="en">
4+
<head>
5+
<meta charset="utf-8">
6+
<title>Bokeh Plot</title>
7+
8+
<link rel="stylesheet" href="https://cdn.pydata.org/bokeh/release/bokeh-0.12.16.min.css" type="text/css" />
9+
10+
<script type="text/javascript" src="https://cdn.pydata.org/bokeh/release/bokeh-0.12.16.min.js"></script>
11+
<script type="text/javascript">
12+
Bokeh.set_log_level("info");
13+
</script>
14+
</head>
15+
<body>
16+
17+
<div class="bk-root">
18+
<div class="bk-plotdiv" id="7bbb049b-2cb4-47e4-b452-8c85b1bfef05"></div>
19+
</div>
20+
21+
<script type="application/json" id="734bca0c-5ec9-4d67-a694-f2461bdaf389">
22+
{"cb8e4306-4b48-454d-bf43-d084109f995f":{"roots":{"references":[{"attributes":{"graph_layout":{"0":[16.276760689518582,70.86899214577603],"1":[41.267732697376246,92.13448024322865],"2":[47.356561047438724,14.173978825970574],"3":[61.76928778670446,97.72233253114625],"4":[84.02834100533148,40.606693950788944],"5":[49.844179779750945,58.686686186377514],"6":[31.048924088051514,76.1274635340279],"7":[70.7569800333874,53.090485260464206]}},"id":"81e06543-8e7b-47b3-ad34-bac0c83d4bb3","type":"StaticLayoutProvider"},{"attributes":{"callback":null,"data":{"names":["0","1","2","3","4","5","6","7"],"x":[16.276760689518582,41.267732697376246,47.356561047438724,61.76928778670446,84.02834100533148,49.844179779750945,31.048924088051514,70.7569800333874],"y":[70.86899214577603,92.13448024322865,14.173978825970574,97.72233253114625,40.606693950788944,58.686686186377514,76.1274635340279,53.090485260464206]},"selected":{"id":"e405dd4c-296f-4860-ae20-76ccf203ed93","type":"Selection"},"selection_policy":{"id":"d4597b81-3489-46ac-bfe6-cab62d9f399b","type":"UnionRenderers"}},"id":"bf33d73c-ed81-441c-b78b-a584c75e35ba","type":"ColumnDataSource"},{"attributes":{"level":"glyph","plot":{"id":"5147bb8d-0b89-4a65-9853-c217147052f8","subtype":"Figure","type":"Plot"},"source":{"id":"bf33d73c-ed81-441c-b78b-a584c75e35ba","type":"ColumnDataSource"},"text":{"field":"names"},"text_align":"center","text_baseline":"middle","x":{"field":"x"},"y":{"field":"y"}},"id":"30fcc2cf-694b-4d7c-8009-726bdd581195","type":"LabelSet"},{"attributes":{"plot":null,"text":"Graph"},"id":"0296772f-5731-4b61-87a5-fde8e211a2a1","type":"Title"},{"attributes":{"callback":null,"end":100},"id":"5ed822d7-f64c-4b0d-9e45-0a70ad191c0d","type":"Range1d"},{"attributes":{"active_drag":"auto","active_inspect":"auto","active_scroll":"auto","active_tap":"auto","tools":[{"id":"efb7e821-430a-432b-9be7-5aef188fff7b","type":"PanTool"},{"id":"7d4a73ba-4aae-47c1-88bf-f3fc0c8419a1","type":"WheelZoomTool"},{"id":"81920c1e-80d3-4c78-83d9-9ba60363160e","type":"BoxZoomTool"},{"id":"0ba5dde9-33ca-499d-a06e-a6063a551fc5","type":"SaveTool"},{"id":"a1082eb0-f39e-4d69-a297-b2982f67bd38","type":"ResetTool"},{"id":"9f13413b-06d9-403d-b7dd-e19d50b077ea","type":"HelpTool"}]},"id":"d0b8e44d-912c-4c65-9b59-7ef380779967","type":"Toolbar"},{"attributes":{},"id":"97892737-4dd3-4857-9b20-4181132b110b","type":"LinearScale"},{"attributes":{},"id":"d8d441d9-6212-4f12-96f0-eaf28fc3eb18","type":"BasicTickFormatter"},{"attributes":{"callback":null,"end":100},"id":"9d5df2ef-11b1-4d97-8edb-66443c48bacc","type":"Range1d"},{"attributes":{},"id":"02c7f48f-454d-4e84-a8f6-7bfe200cc472","type":"NodesOnly"},{"attributes":{},"id":"532f9345-6ca4-418c-9024-2213531372df","type":"LinearScale"},{"attributes":{},"id":"1d818a5a-b3ea-47d5-984b-0d1aeab7f932","type":"BasicTickFormatter"},{"attributes":{"callback":null,"data":{"color":["#18830C","#18830C","#18830C","#18830C","#18830C","#18830C","#18830C","#18830C"],"index":["0","1","2","3","4","5","6","7"]},"selected":{"id":"9faa3719-34ef-4ccd-b7b5-5cf7f7ae1cf1","type":"Selection"},"selection_policy":{"id":"8f84b993-fa62-497e-9ef4-1675fb2e9e1f","type":"UnionRenderers"}},"id":"50834194-b577-4fa1-a065-c74a47126f69","type":"ColumnDataSource"},{"attributes":{"formatter":{"id":"1d818a5a-b3ea-47d5-984b-0d1aeab7f932","type":"BasicTickFormatter"},"plot":{"id":"5147bb8d-0b89-4a65-9853-c217147052f8","subtype":"Figure","type":"Plot"},"ticker":{"id":"3b9f043b-2eb8-4f3c-976e-0bd1c61d65f6","type":"BasicTicker"},"visible":false},"id":"739c61de-b7dc-4986-a03d-c24a9c77ecef","type":"LinearAxis"},{"attributes":{},"id":"3b9f043b-2eb8-4f3c-976e-0bd1c61d65f6","type":"BasicTicker"},{"attributes":{"plot":{"id":"5147bb8d-0b89-4a65-9853-c217147052f8","subtype":"Figure","type":"Plot"},"ticker":{"id":"3b9f043b-2eb8-4f3c-976e-0bd1c61d65f6","type":"BasicTicker"},"visible":false},"id":"5cb2689d-6b24-4843-8031-be4d85f9903d","type":"Grid"},{"attributes":{"formatter":{"id":"d8d441d9-6212-4f12-96f0-eaf28fc3eb18","type":"BasicTickFormatter"},"plot":{"id":"5147bb8d-0b89-4a65-9853-c217147052f8","subtype":"Figure","type":"Plot"},"ticker":{"id":"20436bbe-87c5-42ea-92c1-ee713158328d","type":"BasicTicker"},"visible":false},"id":"bcb8a049-a17f-43a2-be9f-cefc345ef1a8","type":"LinearAxis"},{"attributes":{},"id":"e405dd4c-296f-4860-ae20-76ccf203ed93","type":"Selection"},{"attributes":{},"id":"20436bbe-87c5-42ea-92c1-ee713158328d","type":"BasicTicker"},{"attributes":{},"id":"8db6c82b-bc55-44b9-82e9-294fa45b1506","type":"NodesOnly"},{"attributes":{"below":[{"id":"739c61de-b7dc-4986-a03d-c24a9c77ecef","type":"LinearAxis"}],"left":[{"id":"bcb8a049-a17f-43a2-be9f-cefc345ef1a8","type":"LinearAxis"}],"renderers":[{"id":"739c61de-b7dc-4986-a03d-c24a9c77ecef","type":"LinearAxis"},{"id":"5cb2689d-6b24-4843-8031-be4d85f9903d","type":"Grid"},{"id":"bcb8a049-a17f-43a2-be9f-cefc345ef1a8","type":"LinearAxis"},{"id":"f06a2b26-bd56-409f-8ea0-e87ad8c32152","type":"Grid"},{"id":"2102e127-e2c5-4cb6-aa91-3b2bb7421feb","type":"BoxAnnotation"},{"id":"1347650e-5f2a-4a26-a08d-512004f85cbe","type":"GraphRenderer"},{"id":"30fcc2cf-694b-4d7c-8009-726bdd581195","type":"LabelSet"}],"title":{"id":"0296772f-5731-4b61-87a5-fde8e211a2a1","type":"Title"},"toolbar":{"id":"d0b8e44d-912c-4c65-9b59-7ef380779967","type":"Toolbar"},"x_range":{"id":"5ed822d7-f64c-4b0d-9e45-0a70ad191c0d","type":"Range1d"},"x_scale":{"id":"97892737-4dd3-4857-9b20-4181132b110b","type":"LinearScale"},"y_range":{"id":"9d5df2ef-11b1-4d97-8edb-66443c48bacc","type":"Range1d"},"y_scale":{"id":"532f9345-6ca4-418c-9024-2213531372df","type":"LinearScale"}},"id":"5147bb8d-0b89-4a65-9853-c217147052f8","subtype":"Figure","type":"Plot"},{"attributes":{"dimension":1,"plot":{"id":"5147bb8d-0b89-4a65-9853-c217147052f8","subtype":"Figure","type":"Plot"},"ticker":{"id":"20436bbe-87c5-42ea-92c1-ee713158328d","type":"BasicTicker"},"visible":false},"id":"f06a2b26-bd56-409f-8ea0-e87ad8c32152","type":"Grid"},{"attributes":{},"id":"d4597b81-3489-46ac-bfe6-cab62d9f399b","type":"UnionRenderers"},{"attributes":{"bottom_units":"screen","fill_alpha":{"value":0.5},"fill_color":{"value":"lightgrey"},"left_units":"screen","level":"overlay","line_alpha":{"value":1.0},"line_color":{"value":"black"},"line_dash":[4,4],"line_width":{"value":2},"plot":null,"render_mode":"css","right_units":"screen","top_units":"screen"},"id":"2102e127-e2c5-4cb6-aa91-3b2bb7421feb","type":"BoxAnnotation"},{"attributes":{},"id":"6b47d4ac-53bc-40f1-af0f-705030ec23c4","type":"UnionRenderers"},{"attributes":{"source":{"id":"983e90cf-b2c3-431c-8b7f-3435c4800c5d","type":"ColumnDataSource"}},"id":"bcaeee61-980b-40e0-98e5-99d32addee4b","type":"CDSView"},{"attributes":{},"id":"3b7edf24-a882-4603-88d5-38de4d4f6cc7","type":"Selection"},{"attributes":{"data_source":{"id":"50834194-b577-4fa1-a065-c74a47126f69","type":"ColumnDataSource"},"glyph":{"id":"85d1f431-72b2-4f7b-bc85-077f28a00828","type":"Circle"},"hover_glyph":null,"muted_glyph":null,"view":{"id":"a14e0c9e-d04d-4b16-b7d7-9894827368fa","type":"CDSView"}},"id":"212a7a89-fe84-4d74-b0c4-8e2ac86a614e","type":"GlyphRenderer"},{"attributes":{"data_source":{"id":"983e90cf-b2c3-431c-8b7f-3435c4800c5d","type":"ColumnDataSource"},"glyph":{"id":"e7a61828-eeaf-4b31-939d-7ea2471b753c","type":"MultiLine"},"hover_glyph":null,"muted_glyph":null,"view":{"id":"bcaeee61-980b-40e0-98e5-99d32addee4b","type":"CDSView"}},"id":"a9ffce0f-3c67-41eb-b7d1-aca90c5c0e3e","type":"GlyphRenderer"},{"attributes":{},"id":"9faa3719-34ef-4ccd-b7b5-5cf7f7ae1cf1","type":"Selection"},{"attributes":{},"id":"efb7e821-430a-432b-9be7-5aef188fff7b","type":"PanTool"},{"attributes":{},"id":"8f84b993-fa62-497e-9ef4-1675fb2e9e1f","type":"UnionRenderers"},{"attributes":{"source":{"id":"50834194-b577-4fa1-a065-c74a47126f69","type":"ColumnDataSource"}},"id":"a14e0c9e-d04d-4b16-b7d7-9894827368fa","type":"CDSView"},{"attributes":{},"id":"7d4a73ba-4aae-47c1-88bf-f3fc0c8419a1","type":"WheelZoomTool"},{"attributes":{"overlay":{"id":"2102e127-e2c5-4cb6-aa91-3b2bb7421feb","type":"BoxAnnotation"}},"id":"81920c1e-80d3-4c78-83d9-9ba60363160e","type":"BoxZoomTool"},{"attributes":{},"id":"0ba5dde9-33ca-499d-a06e-a6063a551fc5","type":"SaveTool"},{"attributes":{},"id":"a1082eb0-f39e-4d69-a297-b2982f67bd38","type":"ResetTool"},{"attributes":{},"id":"9f13413b-06d9-403d-b7dd-e19d50b077ea","type":"HelpTool"},{"attributes":{},"id":"e7a61828-eeaf-4b31-939d-7ea2471b753c","type":"MultiLine"},{"attributes":{"fill_color":{"field":"color"},"size":{"units":"screen","value":35}},"id":"85d1f431-72b2-4f7b-bc85-077f28a00828","type":"Circle"},{"attributes":{"edge_renderer":{"id":"a9ffce0f-3c67-41eb-b7d1-aca90c5c0e3e","type":"GlyphRenderer"},"inspection_policy":{"id":"02c7f48f-454d-4e84-a8f6-7bfe200cc472","type":"NodesOnly"},"layout_provider":{"id":"81e06543-8e7b-47b3-ad34-bac0c83d4bb3","type":"StaticLayoutProvider"},"node_renderer":{"id":"212a7a89-fe84-4d74-b0c4-8e2ac86a614e","type":"GlyphRenderer"},"selection_policy":{"id":"8db6c82b-bc55-44b9-82e9-294fa45b1506","type":"NodesOnly"}},"id":"1347650e-5f2a-4a26-a08d-512004f85cbe","type":"GraphRenderer"},{"attributes":{"callback":null,"data":{"end":["0","1","2","3","4","5","6","7"],"start":["0","1","2","3","4","5","6","7"]},"selected":{"id":"3b7edf24-a882-4603-88d5-38de4d4f6cc7","type":"Selection"},"selection_policy":{"id":"6b47d4ac-53bc-40f1-af0f-705030ec23c4","type":"UnionRenderers"}},"id":"983e90cf-b2c3-431c-8b7f-3435c4800c5d","type":"ColumnDataSource"}],"root_ids":["5147bb8d-0b89-4a65-9853-c217147052f8"]},"title":"Bokeh Application","version":"0.12.16"}}
23+
</script>
24+
<script type="text/javascript">
25+
(function() {
26+
var fn = function() {
27+
Bokeh.safely(function() {
28+
(function(root) {
29+
function embed_document(root) {
30+
31+
var docs_json = document.getElementById('734bca0c-5ec9-4d67-a694-f2461bdaf389').textContent;
32+
var render_items = [{"docid":"cb8e4306-4b48-454d-bf43-d084109f995f","elementid":"7bbb049b-2cb4-47e4-b452-8c85b1bfef05","modelid":"5147bb8d-0b89-4a65-9853-c217147052f8"}];
33+
root.Bokeh.embed.embed_items(docs_json, render_items);
34+
35+
}
36+
if (root.Bokeh !== undefined) {
37+
embed_document(root);
38+
} else {
39+
var attempts = 0;
40+
var timer = setInterval(function(root) {
41+
if (root.Bokeh !== undefined) {
42+
embed_document(root);
43+
clearInterval(timer);
44+
}
45+
attempts++;
46+
if (attempts > 100) {
47+
console.log("Bokeh: ERROR: Unable to run BokehJS code because BokehJS library is missing")
48+
clearInterval(timer);
49+
}
50+
}, 10, root)
51+
}
52+
})(window);
53+
});
54+
};
55+
if (document.readyState != "loading") fn();
56+
else document.addEventListener("DOMContentLoaded", fn);
57+
})();
58+
</script>
59+
</body>
60+
</html>

‎graph_dfs_debug/graph.py

+43-19
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
Simple graph implementation compatible with BokehGraph class.
33
"""
44
class Vertex:
5+
"""Vertex needs a label and optionally a connected component"""
56
def __init__(self, label, component=-1):
67
self.label = str(label)
78
self.component = component
@@ -11,45 +12,68 @@ def __repr__(self):
1112

1213
"""Trying to make this Graph class work..."""
1314
class Graph:
15+
"""This Graph will use a dict to store verts, edges"""
1416
def __init__(self):
1517
self.vertices = {}
1618
self.components = 0
1719

1820
def add_vertex(self, vertex, edges=()):
21+
"""Add a vertex, edges optional"""
22+
if vertex in self.vertices:
23+
raise Exception('Error: vertex exists')
24+
if not set(edges).issubset(self.vertices):
25+
raise Exception('Error: no edge possible if both vertices don\'t exist')
1926
self.vertices[vertex] = set(edges)
2027

2128
def add_edge(self, start, end, bidirectional=True):
22-
self.vertices[start].add(start)
29+
"""Add an edge, needs 2 verts, defaults to bidirectional"""
30+
self.vertices[start].add(end)
2331
if bidirectional:
24-
self.vertices[end].add(end)
32+
self.vertices[end].add(start)
2533

26-
def dfs(self, start, target=None):
27-
x = []
28-
x.append(start)
29-
y = set(x)
34+
# def dfs(self, start, target=None):
35+
# x = []
36+
# x.append(start)
37+
# y = set(x)
3038

31-
while x:
32-
z = x.pop()
33-
if x == target:
34-
break
35-
x.extend(self.vertices[z])
39+
# while x:
40+
# z = x.pop()
41+
# if x == target:
42+
# break
43+
# x.extend(self.vertices[z])
44+
45+
# return y
46+
47+
# def graph_rec(self, start, target=None):
48+
# x = set()
49+
# x.add(start)
50+
# for v in self.vertices[start]:
51+
# x.add(graph_rec(v, target))
52+
# return x
3653

37-
return x
54+
def search(self, start, target=None, method='dfs'):
55+
"""Search the graph using BFS or DFS."""
56+
quack = [start] # Queue or stack, depending on method
57+
pop_index = 0 if method == 'bfs' else -1
58+
visited = set()
59+
60+
while quack:
61+
current = quack.pop(pop_index)
62+
if current == target:
63+
break
64+
visited.add(current)
65+
# Add possible (unvisited) vertices to queue
66+
quack.extend(self.vertices[current] - visited)
3867

39-
def graph_rec(self, start, target=None):
40-
x = set()
41-
x.append(start)
42-
for v in self.vertices[start]:
43-
graph_rec(v)
44-
return x
68+
return visited
4569

4670
def find_components(self):
4771
visited = set()
4872
current_component = 0
4973

5074
for vertex in self.vertices:
5175
if vertex in visited:
52-
reachable = self.dfs(vertex)
76+
reachable = self.search(vertex)
5377
for other_vertex in reachable:
5478
other_vertex.component = current_component
5579
current_component += 1

‎graph_dfs_debug/graph_demo.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
"""
44
Demonstration of Graph and BokehGraph functionality.
5+
Debugging for Lambda Week 16 - Graphs
56
"""
67

78
from random import sample
@@ -34,4 +35,9 @@ def main(num_vertices=8, num_edges=8, draw_components=True):
3435
DRAW_COMPONENTS = bool(int(argv[3]))
3536
main(NUM_VERTICES, NUM_EDGES, DRAW_COMPONENTS)
3637
else:
37-
main()
38+
print('**** ERROR ****')
39+
print("NUM_VERTICES:", NUM_VERTICES)
40+
print("NUM_EDGES:", NUM_EDGES)
41+
print("DRAW_COMPONENTS:", DRAW_COMPONENTS)
42+
print('Expected arguments: num_vertices num_edges draw_components')
43+
print('Both numbers should be integers, draw_components should be 0/1')

0 commit comments

Comments
 (0)
Please sign in to comment.