<jupyter_start><jupyter_text><jupyter_code>import matplotlib.pyplot as plt
import numpy as np 
import networkx as nx<jupyter_output><empty_output><jupyter_text>Example 1<jupyter_code>G = nx.Graph()
city_set = ['San Francisco', 'New York', 'Chicago', 'Philadelphia', 'Boston',
            "Andhra Pradesh","Arunachal Pradesh ","Assam","Bihar","Chhattisgarh",
            "Goa","Gujarat","Haryana","Himachal Pradesh","Jammu and Kashmir",
            "Jharkhand","Karnataka","Kerala","Madhya Pradesh","Maharashtra",
            "Manipur","Meghalaya","Mizoram","Nagaland","Odisha","Punjab",
            "Rajasthan","Sikkim","Tamil Nadu","Telangana","Tripura",
            "Uttar Pradesh","Uttarakhand","West Bengal","Andaman and Nicobar Islands",
            "Chandigarh","Dadra and Nagar Haveli","Daman and Diu","Lakshadweep",
            "National Capital Territory of Delhi","Puducherry"]
for city in city_set:
    G.add_node(city)
G.nodes()
G.edges()
nx.draw(G , with_labels = 1)
nx.draw_networkx(G)<jupyter_output><empty_output><jupyter_text>2nd Example<jupyter_code>import random
H = nx.Graph()
city_set1 = ['New York', 'Chicago', 'Boston',"Assam","Bihar","Chhattisgarh","Goa","Gujarat","Haryana"]
for each in city_set1:
    H.add_node(each)
print(H.size())    # size of the graph 
print(H.order())   # number of vertices in the graph 
H.nodes()
nx.draw(H , with_labels = 1 )
print(H.number_of_nodes())
print(H.number_of_edges())
costs = []
c = 100 
while(c<=2000):
    costs.append(c)
    c+=100
print(costs)
k = list(H.nodes())
k
while(H.number_of_edges()<10 ):
    c1 = random.choice(k)
    c2 = random.choice(k)
    if c1!=c2 and H.has_edge(c1 ,c2)==0:
        w = random.choice(costs)
        H.add_edge(c1,c2,weight = w)        
nx.draw(H , with_labels = 1)
        
pos = nx.spectral_layout(H)
nx.draw(H, pos,with_labels = 1)
pos = nx.circular_layout(H)
nx.draw(H, pos,with_labels = 1)
nx.is_connected(H)
for u in H.nodes():
    for v in H.nodes():
        print(u,v , nx.has_path(H,u,v))  # we csan't go anywhere from Newyork .<jupyter_output>New York New York True
New York Chicago False
New York Boston False
New York Assam False
New York Bihar False
New York Chhattisgarh False
New York Goa False
New York Gujarat False
New York Haryana False
Chicago New York False
Chicago Chicago True
Chicago Boston True
Chicago Assam True
Chicago Bihar True
Chicago Chhattisgarh True
Chicago Goa True
Chicago Gujarat True
Chicago Haryana True
Boston New York False
Boston Chicago True
Boston Boston True
Boston Assam True
Boston Bihar True
Boston Chhattisgarh True
Boston Goa True
Boston Gujarat True
Boston Haryana True
Assam New York False
Assam Chicago True
Assam Boston True
Assam Assam True
Assam Bihar True
Assam Chhattisgarh True
Assam Goa True
Assam Gujarat True
Assam Haryana True
Bihar New York False
Bihar Chicago True
Bihar Boston True
Bihar Assam True
Bihar Bihar True
Bihar Chhattisgarh True
Bihar Goa True
Bihar Gujarat True
Bihar Haryana True
Chhattisgarh New York False
Chhattisgarh Chicago True
Chhattisgarh Boston True
Chhattisgarh As[...]<jupyter_text>**Find the shortest path between two cities **<jupyter_code>print(nx.dijkstra_path_length(H,'Goa','Bihar'))
nx.dijkstra_path(H,'Goa','Bihar')<jupyter_output><empty_output><jupyter_text>Example 3<jupyter_code>import networkx as nx 
import matplotlib.pyplot as plt 
import random 

G=nx.Graph() 
city_list=['a','b','c','d','e','f','g','h']

# this is a bit easier then adding each node in a loop 
G.add_nodes_from(city_list)
print(G.nodes())
print(type(G.nodes()))
G
# based on your old code:    
for _ in city_list: 
    ab=random.choice(city_list) 
    print(ab)
# generate n random edges
n=5
for _ in range(n):
    # random.sample(city_list, 2) gives a 2-tuple from city list
    # The '*'-operator unpacks the tuple to two input values for the .add_edge() method
    G.add_edge(*random.sample(city_list, 2))
print("Edges generated:", G.edges())
nx.draw(G , with_labels = 1)
pos = nx.spectral_layout(G)
nx.draw(G, pos,with_labels = 1)
pos = nx.circular_layout(G)
nx.draw(G, pos,with_labels = 1)
pos = nx.circular_layout(G)
nx.draw(G , with_labels = 1)
nx.draw_networkx_edge_labels(G,pos)
plt.show()
nx.is_connected(G)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12417061611374408, 'fraction_numerical': 0.00924170616113744, 'mean_word_length': 4.798076923076923, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '217268', 'license': 'MIT', 'n_tokens_mistral': 1655, 'n_tokens_neox': 1566, 'n_words': 411, 'path': 'networkx3.ipynb', 'repo_name': 'priyanshgupta1998/Networkx', 'size': '4220'}