Assignment : “hello, graph world”

We are going to show the Krackhardt kite in the assignment. We are using Networkx library to create the grapgh object and matplotlib to show the actual plot.

We loads the correct libraries needed

import networkx as nx  # load the networkx library to create the graph object 
import matplotlib.pyplot as plt 

We are going to create the nodes and the edges below:

# create the nodes 
GG = nx.Graph()

nodes = ["Andre","Beverly", "Carol", "Diane", "Ed", 
         "Fernando", "Garth", "Heather", "Ike", "Jane"] # this is all the name that are goijng to be the nodes 
GG.add_nodes_from(nodes)
# Adding all the connection to the nodes (edges)

edges = [ 
    ("Andre", "Beverly"), ("Andre", "Carol"), ("Andre", "Diane"),
    ("Beverly", "Diane"), ("Beverly", "Ed"),
    ("Carol", "Diane"), ("Carol", "Fernando"),
    ("Diane", "Ed"), ("Diane", "Fernando"), ("Diane", "Garth"),
    ("Ed", "Garth"),
    ("Fernando", "Garth"), ("Fernando", "Heather"),
    ("Garth", "Heather"),
    ("Heather", "Ike"),
    ("Ike", "Jane")
]
GG.add_edges_from(edges)

Before we create the plot, let check how many edges and nodes are the GG objects.

# number of nodes 
print(f" we have the {GG.number_of_nodes()} nodes for the the Krackhardt kite plot")
##  we have the 10 nodes for the the Krackhardt kite plot
list(nx.connected_components(GG))
## [{'Heather', 'Carol', 'Ed', 'Ike', 'Andre', 'Fernando', 'Garth', 'Jane', 'Beverly', 'Diane'}]
print(f" We have {GG.number_of_edges()} edges for the the Krackhardt kite plot")
##  We have 16 edges for the the Krackhardt kite plot
GG.edges # This show all the connections
## EdgeView([('Andre', 'Beverly'), ('Andre', 'Carol'), ('Andre', 'Diane'), ('Beverly', 'Diane'), ('Beverly', 'Ed'), ('Carol', 'Diane'), ('Carol', 'Fernando'), ('Diane', 'Ed'), ('Diane', 'Fernando'), ('Diane', 'Garth'), ('Ed', 'Garth'), ('Fernando', 'Garth'), ('Fernando', 'Heather'), ('Garth', 'Heather'), ('Heather', 'Ike'), ('Ike', 'Jane')])
# plot the grapgh for GG 
plt.figure(figsize=(10,8))
pos = nx.spring_layout(GG, seed = 42) 

nx.draw(GG, pos, with_labels=True, node_color = 'violet',
        node_size= 2000, font_size = 10, font_weight = 'bold',
        edge_color='purple')
plt.title("Krackhardt Kite Grapgh", fontsize = 14)
plt.show()

The link is my Github