The Graph Theory - An Introduction in Python
The Graph Theory - An Introduction in Python
1 of 10 4/25/2021, 3:21 PM
The Graph Theory — An Introduction In Python | by Sofiyan Sheikh | A... https://medium.com/apprentice-journal/the-graph-theory-an-introduction-...
2 of 10 4/25/2021, 3:21 PM
The Graph Theory — An Introduction In Python | by Sofiyan Sheikh | A... https://medium.com/apprentice-journal/the-graph-theory-an-introduction-...
3 of 10 4/25/2021, 3:21 PM
The Graph Theory — An Introduction In Python | by Sofiyan Sheikh | A... https://medium.com/apprentice-journal/the-graph-theory-an-introduction-...
4 of 10 4/25/2021, 3:21 PM
The Graph Theory — An Introduction In Python | by Sofiyan Sheikh | A... https://medium.com/apprentice-journal/the-graph-theory-an-introduction-...
<object>.nodes() <object>.edges()
print(P.nodes())
print(P.edges())
nx.draw
%matplotlib inline
import matplotlib.pyplot as plt
nx.draw(P, with_labels = True)
5 of 10 4/25/2021, 3:21 PM
The Graph Theory — An Introduction In Python | by Sofiyan Sheikh | A... https://medium.com/apprentice-journal/the-graph-theory-an-introduction-...
#import packages
import pandas as pd
import numpy as np
#review data
file.head()
6 of 10 4/25/2021, 3:21 PM
The Graph Theory — An Introduction In Python | by Sofiyan Sheikh | A... https://medium.com/apprentice-journal/the-graph-theory-an-introduction-...
<object>.edges() nx.draw
import networkx as nx
FG = nx.from_pandas_edgelist(file2, source='ORIGIN_AIRPORT',
target='DESTINATION_AIRPORT', edge_attr=True)
FG.nodes()
FG.edges()
7 of 10 4/25/2021, 3:21 PM
The Graph Theory — An Introduction In Python | by Sofiyan Sheikh | A... https://medium.com/apprentice-journal/the-graph-theory-an-introduction-...
nx.algorithms.degree_centrality(<object>)
8 of 10 4/25/2021, 3:21 PM
The Graph Theory — An Introduction In Python | by Sofiyan Sheikh | A... https://medium.com/apprentice-journal/the-graph-theory-an-introduction-...
nx.dijkstra_path
nx.draw()
9 of 10 4/25/2021, 3:21 PM
The Graph Theory — An Introduction In Python | by Sofiyan Sheikh | A... https://medium.com/apprentice-journal/the-graph-theory-an-introduction-...
# Add a node
NG.add_nodes_from(['A','B','C','D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L'])
# Add a list of nodes by passing a list argument
# Add edges
NG.add_edges_from([('A', 'B'),('C','E'),('D','E'), ('F','G'), ('F','H'),
('G', 'J'), ('H', 'J'), ('H', 'M'), ('H','L'), ('M', 'L'), ('J', 'K'), ('J',
'G'), ('K', 'I'), ('I', 'G'), ('L', 'K')])
#show nodes
NG = nx.from_pandas_edgelist(ntwrk, source='source', target='target',
edge_attr=True)
NG.nodes()
#show edges
NG.edges()
#draw graph
nx.draw(NG, with_labels = True)
#shortest path
djk_path = nx.dijkstra_path(NG, source='L', target='F')
djk_path
10 of 10 4/25/2021, 3:21 PM