Dictionary Graph
Dictionary Graph
import numpy as np
## GRAPHS / TREES
# Using Pandas
matrixpd=pd.DataFrame([[1,2,3],[23,45,67],[34,565,677]])
results=matrixpd.copy()
graph = np.array([[0,1,1],[1,0,1],[1,1,0]])
#DEgreen of a Node is the sum of its row values in the adjacent matrix
degree=np.sum(graph, axis=1) # degree
# Path Counting
# The Square of Adjacent Matrix gives the number of paths of length 2 between
nodes
#For 2D arrays (matrices):
#Both np.dot(A, A) and np.matmul(A, A) work similarly,
# but np.matmul() is clearer and more explicit in its intent.
results=paths_3.copy()
# (A)---(B)
# | /
# | /
# (C)
graph2= {"A": ["B","C"], "B":["A","C"],"C":["A","B"]}
results= graph2
if 'B' in graph2["A"]:
results="There is an edge between A and B"
else:
results="No edge between A and B"
graph2 = {
"A": ["B", "D"],
"B": ["A", "C", "D"],
"C": ["B"],
"D": ["A", "B"]
}
results=[]
for node, neighbors in graph2.items(): # This goes through each nodes ,
neighbors stores all neighbors
for neighbor in neighbors: #This looks at all directly connected
neighbors
for second_neighbor in graph2[neighbor]: #this looks at the neighbor
of the first neighbors
# If we started at A and went to B and then went back to A we dont count that
if second_neighbor !=node:
#print (f"path from {node} to {second_neighbor}")
results.append(f"path from {node} to {second_neighbor}")
#### GRAPHS IN DETAILS
results=[]
for node, neighbor1 in graph2.items():
results.append(f"{node} --> {neighbor1}")
#Traversing a Graphs
# What is Graph Traversal
# Graph Traversal is the process of visiting all nodes of a graph in a
systematic way
# It helps to:
# Find connections between nodes
# Search for specific elements
# Solve complex problems path finding problems
print( f"{results}")
matrix()
graphr = {
"A": ["B", "D"],
"B": ["A", "C", "D"],
"C": ["B"],
"D": ["A", "B"]
}
# DEPT FIRST SEARCh:
if visited is None:
visited=set() # Create an empty set to track the visited Node
dfs(graphr,"A")
graphr = {
"A": ["B", "D"],
# "B": ["A", "C", "D"],
"C": ["B"],
"D": ["A", "B"]
}
#def say_hello():
# print('Hello, World')
#for i in range(5):
# say_hello()