import networkx as nx
import matplotlib.pyplot as plt

# read in our graph, as a list of edges
G = nx.read_edgelist("lec01.data")

G.order()   # number of vertices
G.size()    # number of edges
G.nodes     # list of vertices
G.edges     # list of edges
G.adj       # adjacencies of each vertex

# Draw and display our graph topology
nx.draw(G, with_labels=True)
plt.show()

# Run our connectivity algorithm
# First, set each vertex state to a unique identifier
counter = 0
S = {}
for v in G.nodes:
    S[v] = counter
    counter += 1

# Iteratively propagate max state values across edges
# Continue as long as we propagate at least one value
updates = 1
while updates > 0:
    updates = 0
    for v in G.nodes:
        for u in G.neighbors(v):
            if S[u] > S[v]:
                S[v] = S[u]
                updates += 1

# Bit of a hack for visualization - won't generalize to other graph inputs
colors = []
for v in G.nodes:
    if S[v] == 7:
        colors.append('green')
    elif S[v] == 4:
        colors.append('red')

# Draw our graph with components labeled via coloring hack
nx.draw(G, with_labels=True, node_color=colors)
plt.show()



