graph list Algorithm
A distinction is made between undirected graphs, where edges link two vertices symmetrically, and directed graphs, where edges link two vertices asymmetrically; see graph (discrete mathematics) for more detailed definitions and for other variations in the types of graph that are normally considered. In mathematics, graph theory is the survey of graphs, which are mathematical structures used to model pairwise relations between objects.
The introduction of probabilistic methods in graph theory, particularly in the survey of Erdős and Rényi of the asymptotic probability of graph connectivity, give rise to yet another branch, known as random graph theory, which has been a fruitful source of graph-theoretic outcomes. The works of Ramsey on colorations and more especially the outcomes obtained by Turán in 1941 was at the origin of another branch of graph theory, extremal graph theory. The techniques he used chiefly relate the enumeration of graphs with particular property.
#!/usr/bin/python
# Author: OMKAR PATHAK
# We can use Python's dictionary for constructing the graph.
class AdjacencyList:
def __init__(self):
self.List = {}
def addEdge(self, fromVertex, toVertex):
# check if vertex is already present
if fromVertex in self.List.keys():
self.List[fromVertex].append(toVertex)
else:
self.List[fromVertex] = [toVertex]
def printList(self):
for i in self.List:
print((i, "->", " -> ".join([str(j) for j in self.List[i]])))
if __name__ == "__main__":
al = AdjacencyList()
al.addEdge(0, 1)
al.addEdge(0, 4)
al.addEdge(4, 1)
al.addEdge(4, 3)
al.addEdge(1, 0)
al.addEdge(1, 4)
al.addEdge(1, 3)
al.addEdge(1, 2)
al.addEdge(2, 3)
al.addEdge(3, 4)
al.printList()
# OUTPUT:
# 0 -> 1 -> 4
# 1 -> 0 -> 4 -> 3 -> 2
# 2 -> 3
# 3 -> 4
# 4 -> 1 -> 3