-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathlab7.py
More file actions
221 lines (181 loc) · 5.54 KB
/
lab7.py
File metadata and controls
221 lines (181 loc) · 5.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
"""Data Structures
Working with Graphs/Networks"""
import math
def makeLink(G, node1, node2):
if node1 not in G:
G[node1] = {}
(G[node1])[node2] = 1
if node2 not in G:
G[node2] = {}
(G[node2])[node1] = 1
return G
# Ring Network
ring = {} # empty graph
n = 5 # number of nodes
# Add in edges
for i in range(n):
ring = makeLink(ring, i, (i+1)%n)
# How many nodes?
print len(ring)
# How many edges?
print sum([len(ring[node]) for node in ring.keys()])/2
left_side1=[]
left_side2=[]
right_side=[]
left_side1=[16, 32,48,64,80, 96, 112, 128,144,160,176,192, 208, 224]
#left_side2=[32,64,96,128,160,192,224]
right_side1=[15,31,47,63,79,95,111, 127,143, 159, 175, 191, 207, 223, 239]
#right_side2=[47,79,111,143,175,207,239]
def class_net(G, nodes):
msn=int(math.sqrt(nodes))
msnm=int(math.sqrt(nodes)-1)
msnp=int(math.sqrt(nodes)+1)
for i in range(nodes):
if i==0:
makeLink(G, i, (i+1))
makeLink(G, i, (i+msn))
elif i==(math.sqrt(nodes)-1):
makeLink(G, i, (i-1))
makeLink(G, i, (i+msn))
elif i==(nodes-1):
makeLink(G, i, (i-1))
makeLink(G, i, (i-msn))
elif i==((nodes-1)-(msn-1)):
makeLink(G, i, (i+1))
makeLink(G, i, (i-msn))
elif i in range(1,msn-1):
makeLink(G, i, (i+1))
elif i in range(nodes-msn+1,nodes-1):
makeLink(G, i, (i+1))
elif i in left_side1:
makeLink(G, i, (i+msn))
elif i in right_side1:
makeLink(G, i, (i+msn))
else:
makeLink(G, i, (i+1))
makeLink(G, i, (i-1))
makeLink(G, i, (i+msn))
makeLink(G, i, (i-msn))
print left_side1
print right_side1
# Grid Network
# TODO: create a square graph with 256 nodes and count the edges
####Making square graph
class Node():
def __init__(self,value=None):
self.value=value
self.parent=None
self.children=[None,None]
def __repr__(self):
return "Node object with value %s" %(self.value)
def __str__(self):
if self.children !=(None,None):
return "Node value: %s \n left child: \n %s \n right child: \n %s" %(self.value,self.children[0],self.children[1])
else: return "Node value: %s" %self.value
# TODO: define a function countEdges
def countEdges(G):
edges=sum([len(G[node]) for node in G.keys()])/2
print edges
#257
import networkx as nx
import matplotlib.pyplot as plt
def draw_graph(graph):
# extract nodes from graph
nodes = set([n1 for n1, n2 in graph] + [n2 for n1, n2 in graph])
# create networkx graph
G=nx.Graph()
# add nodes
for node in nodes:
G.add_node(node)
# add edges
for edge in graph:
G.add_edge(edge[0], edge[1])
# draw graph
pos = nx.shell_layout(G)
nx.draw(G, pos)
# show graph
plt.show()
# Social Network
class Actor(object):
def __init__(self, name):
self.name = name
def __repr__(self):
return self.name
ss = Actor("Susan Sarandon")
jr = Actor("Julia Roberts")
kb = Actor("Kevin Bacon")
ah = Actor("Anne Hathaway")
rd = Actor("Robert DiNero")
ms = Actor("Meryl Streep")
dh = Actor("Dustin Hoffman")
movies = {}
makeLink(movies, dh, rd) # Wag the Dog
makeLink(movies, rd, ms) # Marvin's Room
makeLink(movies, dh, ss) # Midnight Mile
makeLink(movies, dh, jr) # Hook
makeLink(movies, dh, kb) # Sleepers
makeLink(movies, ss, jr) # Stepmom
makeLink(movies, kb, jr) # Flatliners
makeLink(movies, kb, ms) # The River Wild
makeLink(movies, ah, ms) # Devil Wears Prada
makeLink(movies, ah, jr) # Valentine's Day
# How many nodes in movies?
7
# How many edges in movies?
10
def tour(graph, nodes):
for i in range(len(nodes)):
node = nodes[i]
if node in graph.keys():
print node
else:
print "Node not found!"
break
if i+1 < len(nodes):
next_node = nodes[i+1]
if next_node in graph.keys():
if next_node in graph[node].keys():
pass
else:
print "Can't get there from here!"
break
# TODO: find an Eulerian tour of the movie network and check it
movie_tour = []
tour(movies, movie_tour)
def findPath(graph, start, end, path=[]):
path = path + [start]
if start == end:
return path
if not graph.has_key(start):
return None
for node in graph[start]:
if node not in path:
newpath = findPath(graph, node, end, path)
if newpath: return newpath
return None
print findPath(movies, jr, ms)
# TODO: implement findShortestPath()
# print findShortestPath(movies, ms, ss)
# TODO: implement findAllPaths() to find all paths between two nodes
# allPaths = findAllPaths(movies, jr, ms)
# for path in allPaths:
# print path
# Copyright (c) 2014 Matt Dickenson
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.