# from last time
import numpy as np
def rand_graph(V,E):
"""Generates random graph described above,
with V vertices and E edges. Returns
adjacency matrix A."""
A = np.zeros((V,V)) # initialize to all zeros
edges_so_far = 0 # counts edges added so far
while edges_so_far < E:
i = randint(0,V-1)
j = randint(0,V-1)
if (i != j and A[i,j] == 0):
# above tests whether i,j are different
# and no edge already between them
A[i,j] = 1 # add edge between i,j
A[j,i] = 1 # keep adjacency matrix symmetric
edges_so_far += 1 # increment number of edges added
return A
# from last time
def deg_seq(A):
"""Returns list of degrees of vertices
in graph represented by adj matrix A."""
# sum(A[w]) equals degree of w
return [sum(A[w]) for w in range(len(A))]
# note len(A) is equal to number of rows of A
# (which equals number of vertices)
# Want to compare degree seq to Poisson
# with intensity lam = 2E/V
# Below will make list_plot of degree sequence, which
# we will plot on same graph as Poisson pmf next class
V = 1000
E = 1000
lam = 2*E/V
A = rand_graph(V,E)
deg = deg_seq(A)
deg_table = np.zeros(E)
# deg_table[i] will be num of vertices with deg=i
#print(deg)
for d in deg:
deg_table[int(d)] += 1/V
#print(deg_table)
list_plot(deg_table[0:30], color="red", marker="<")+\
list_plot([lam^k * exp(-lam)/factorial(k) for k in range(30)])
# red markers give degree sequence for the random graph
# blue is the pmf for Poisson lambda
# the two distributions are very close (as V gets large)
# Code to determine whether graph is connected
# This is similar to your task for Project 3
# We will use a Depth First Search (using recursion)
# will represent graph with adjacency matrix A
visited = [] # will keep track of which vertices have been visited
def is_connected(A):
"""Returns True iff the graph corresponding to
adjacency matrix A is connected."""
global visited # use global copy of visited defined outside function
# initialize visited to all unvisited
visited = [False for i in range(len(A))]
# since visited is global, this changes the
# value outside of the function as well
# visited[i] will be set True if i is visited
explore(A,0)
if False in visited:
# test if some vertex is unvisited
return False
else:
return True
def explore(A,v):
"""Explore all the vertices accesible from v
that have not been visited yet. Mark all of
these as visited."""
global visited
visited[v]=True
print(v) # print out vertices as they're visited
# (just for testing; should get rid of for large graphs)
for w in range(len(A)):
# iterate over all vertices
if A[v,w]==1 and visited[w]==False:
# get here for all w unvisited
# that neighbor v
explore(A,w)
import networkx as nx
A = rand_graph(8,7)
nx.draw(nx.Graph(A))
is_connected(A) # test whether above graph is connected
# example graph from white board
A_ex = np.zeros((5,5))
A_ex[0,1]=A_ex[0,2]=1
A_ex[1,0]=A_ex[2,0]=1
A_ex[1,3]=A_ex[1,4]=1
A_ex[3,1]=A_ex[4,1]=1
A_ex[1,3]=1
A_ex[2,4]=1
A_ex[4,2]=1
A_ex
nx.draw(nx.Graph(A_ex))
is_connected(A_ex)
import networkx as nx
A = rand_graph(20,30)
nx.draw(nx.Graph(A))
is_connected(A)
import networkx as nx
A = rand_graph(20,30)
nx.draw(nx.Graph(A))
is_connected(A)
import networkx as nx
A = rand_graph(1000,1000)
#nx.draw(nx.Graph(A))
is_connected(A)