mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-02-25 18:38:39 +00:00
Travis CI: Add pytest --doctest-modules graphs (#1018)
This commit is contained in:
parent
267b5eff40
commit
7cdda931fd
@ -21,6 +21,7 @@ script:
|
|||||||
digital_image_processing
|
digital_image_processing
|
||||||
divide_and_conquer
|
divide_and_conquer
|
||||||
dynamic_programming
|
dynamic_programming
|
||||||
|
graphs
|
||||||
hashes
|
hashes
|
||||||
linear_algebra_python
|
linear_algebra_python
|
||||||
matrix
|
matrix
|
||||||
|
@ -10,40 +10,42 @@ try:
|
|||||||
except NameError:
|
except NameError:
|
||||||
xrange = range # Python 3
|
xrange = range # Python 3
|
||||||
|
|
||||||
# Accept No. of Nodes and edges
|
|
||||||
n, m = map(int, raw_input().split(" "))
|
|
||||||
|
|
||||||
# Initialising Dictionary of edges
|
if __name__ == "__main__":
|
||||||
g = {}
|
# Accept No. of Nodes and edges
|
||||||
for i in xrange(n):
|
n, m = map(int, raw_input().split(" "))
|
||||||
|
|
||||||
|
# Initialising Dictionary of edges
|
||||||
|
g = {}
|
||||||
|
for i in xrange(n):
|
||||||
g[i + 1] = []
|
g[i + 1] = []
|
||||||
|
|
||||||
"""
|
"""
|
||||||
--------------------------------------------------------------------------------
|
----------------------------------------------------------------------------
|
||||||
Accepting edges of Unweighted Directed Graphs
|
Accepting edges of Unweighted Directed Graphs
|
||||||
--------------------------------------------------------------------------------
|
----------------------------------------------------------------------------
|
||||||
"""
|
"""
|
||||||
for _ in xrange(m):
|
for _ in xrange(m):
|
||||||
x, y = map(int, raw_input().split(" "))
|
x, y = map(int, raw_input().strip().split(" "))
|
||||||
g[x].append(y)
|
g[x].append(y)
|
||||||
|
|
||||||
"""
|
"""
|
||||||
--------------------------------------------------------------------------------
|
----------------------------------------------------------------------------
|
||||||
Accepting edges of Unweighted Undirected Graphs
|
Accepting edges of Unweighted Undirected Graphs
|
||||||
--------------------------------------------------------------------------------
|
----------------------------------------------------------------------------
|
||||||
"""
|
"""
|
||||||
for _ in xrange(m):
|
for _ in xrange(m):
|
||||||
x, y = map(int, raw_input().split(" "))
|
x, y = map(int, raw_input().strip().split(" "))
|
||||||
g[x].append(y)
|
g[x].append(y)
|
||||||
g[y].append(x)
|
g[y].append(x)
|
||||||
|
|
||||||
"""
|
"""
|
||||||
--------------------------------------------------------------------------------
|
----------------------------------------------------------------------------
|
||||||
Accepting edges of Weighted Undirected Graphs
|
Accepting edges of Weighted Undirected Graphs
|
||||||
--------------------------------------------------------------------------------
|
----------------------------------------------------------------------------
|
||||||
"""
|
"""
|
||||||
for _ in xrange(m):
|
for _ in xrange(m):
|
||||||
x, y, r = map(int, raw_input().split(" "))
|
x, y, r = map(int, raw_input().strip().split(" "))
|
||||||
g[x].append([y, r])
|
g[x].append([y, r])
|
||||||
g[y].append([x, r])
|
g[y].append([x, r])
|
||||||
|
|
||||||
@ -168,9 +170,10 @@ def topo(G, ind=None, Q=[1]):
|
|||||||
|
|
||||||
|
|
||||||
def adjm():
|
def adjm():
|
||||||
n, a = raw_input(), []
|
n = raw_input().strip()
|
||||||
|
a = []
|
||||||
for i in xrange(n):
|
for i in xrange(n):
|
||||||
a.append(map(int, raw_input().split()))
|
a.append(map(int, raw_input().strip().split()))
|
||||||
return a, n
|
return a, n
|
||||||
|
|
||||||
|
|
||||||
|
@ -34,21 +34,21 @@ def BellmanFord(graph, V, E, src):
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
#MAIN
|
if __name__ == "__main__":
|
||||||
V = int(input("Enter number of vertices: "))
|
V = int(input("Enter number of vertices: ").strip())
|
||||||
E = int(input("Enter number of edges: "))
|
E = int(input("Enter number of edges: ").strip())
|
||||||
|
|
||||||
graph = [dict() for j in range(E)]
|
graph = [dict() for j in range(E)]
|
||||||
|
|
||||||
for i in range(V):
|
for i in range(V):
|
||||||
graph[i][i] = 0.0
|
graph[i][i] = 0.0
|
||||||
|
|
||||||
for i in range(E):
|
for i in range(E):
|
||||||
print("\nEdge ",i+1)
|
print("\nEdge ",i+1)
|
||||||
src = int(input("Enter source:"))
|
src = int(input("Enter source:").strip())
|
||||||
dst = int(input("Enter destination:"))
|
dst = int(input("Enter destination:").strip())
|
||||||
weight = float(input("Enter weight:"))
|
weight = float(input("Enter weight:").strip())
|
||||||
graph[i] = {"src": src,"dst": dst, "weight": weight}
|
graph[i] = {"src": src,"dst": dst, "weight": weight}
|
||||||
|
|
||||||
gsrc = int(input("\nEnter shortest path source:"))
|
gsrc = int(input("\nEnter shortest path source:").strip())
|
||||||
BellmanFord(graph, V, E, gsrc)
|
BellmanFord(graph, V, E, gsrc)
|
||||||
|
@ -37,21 +37,21 @@ def Dijkstra(graph, V, src):
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
#MAIN
|
if __name__ == "__main__":
|
||||||
V = int(input("Enter number of vertices: "))
|
V = int(input("Enter number of vertices: ").strip())
|
||||||
E = int(input("Enter number of edges: "))
|
E = int(input("Enter number of edges: ").strip())
|
||||||
|
|
||||||
graph = [[float('inf') for i in range(V)] for j in range(V)]
|
graph = [[float('inf') for i in range(V)] for j in range(V)]
|
||||||
|
|
||||||
for i in range(V):
|
for i in range(V):
|
||||||
graph[i][i] = 0.0
|
graph[i][i] = 0.0
|
||||||
|
|
||||||
for i in range(E):
|
for i in range(E):
|
||||||
print("\nEdge ",i+1)
|
print("\nEdge ",i+1)
|
||||||
src = int(input("Enter source:"))
|
src = int(input("Enter source:").strip())
|
||||||
dst = int(input("Enter destination:"))
|
dst = int(input("Enter destination:").strip())
|
||||||
weight = float(input("Enter weight:"))
|
weight = float(input("Enter weight:").strip())
|
||||||
graph[src][dst] = weight
|
graph[src][dst] = weight
|
||||||
|
|
||||||
gsrc = int(input("\nEnter shortest path source:"))
|
gsrc = int(input("\nEnter shortest path source:").strip())
|
||||||
Dijkstra(graph, V, gsrc)
|
Dijkstra(graph, V, gsrc)
|
||||||
|
@ -1,32 +1,34 @@
|
|||||||
from __future__ import print_function
|
from __future__ import print_function
|
||||||
num_nodes, num_edges = list(map(int,input().split()))
|
|
||||||
|
|
||||||
edges = []
|
if __name__ == "__main__":
|
||||||
|
num_nodes, num_edges = list(map(int, input().strip().split()))
|
||||||
|
|
||||||
for i in range(num_edges):
|
edges = []
|
||||||
node1, node2, cost = list(map(int,input().split()))
|
|
||||||
|
for i in range(num_edges):
|
||||||
|
node1, node2, cost = list(map(int, input().strip().split()))
|
||||||
edges.append((i,node1,node2,cost))
|
edges.append((i,node1,node2,cost))
|
||||||
|
|
||||||
edges = sorted(edges, key=lambda edge: edge[3])
|
edges = sorted(edges, key=lambda edge: edge[3])
|
||||||
|
|
||||||
parent = [i for i in range(num_nodes)]
|
parent = list(range(num_nodes))
|
||||||
|
|
||||||
def find_parent(i):
|
def find_parent(i):
|
||||||
if(i != parent[i]):
|
if i != parent[i]:
|
||||||
parent[i] = find_parent(parent[i])
|
parent[i] = find_parent(parent[i])
|
||||||
return parent[i]
|
return parent[i]
|
||||||
|
|
||||||
minimum_spanning_tree_cost = 0
|
minimum_spanning_tree_cost = 0
|
||||||
minimum_spanning_tree = []
|
minimum_spanning_tree = []
|
||||||
|
|
||||||
for edge in edges:
|
for edge in edges:
|
||||||
parent_a = find_parent(edge[1])
|
parent_a = find_parent(edge[1])
|
||||||
parent_b = find_parent(edge[2])
|
parent_b = find_parent(edge[2])
|
||||||
if(parent_a != parent_b):
|
if parent_a != parent_b:
|
||||||
minimum_spanning_tree_cost += edge[3]
|
minimum_spanning_tree_cost += edge[3]
|
||||||
minimum_spanning_tree.append(edge)
|
minimum_spanning_tree.append(edge)
|
||||||
parent[parent_a] = parent_b
|
parent[parent_a] = parent_b
|
||||||
|
|
||||||
print(minimum_spanning_tree_cost)
|
print(minimum_spanning_tree_cost)
|
||||||
for edge in minimum_spanning_tree:
|
for edge in minimum_spanning_tree:
|
||||||
print(edge)
|
print(edge)
|
||||||
|
@ -100,12 +100,13 @@ def PrimsAlgorithm(l):
|
|||||||
Nbr_TV[ v[0] ] = vertex
|
Nbr_TV[ v[0] ] = vertex
|
||||||
return TreeEdges
|
return TreeEdges
|
||||||
|
|
||||||
# < --------- Prims Algorithm --------- >
|
if __name__ == "__main__":
|
||||||
n = int(input("Enter number of vertices: "))
|
# < --------- Prims Algorithm --------- >
|
||||||
e = int(input("Enter number of edges: "))
|
n = int(input("Enter number of vertices: ").strip())
|
||||||
adjlist = defaultdict(list)
|
e = int(input("Enter number of edges: ").strip())
|
||||||
for x in range(e):
|
adjlist = defaultdict(list)
|
||||||
l = [int(x) for x in input().split()]
|
for x in range(e):
|
||||||
|
l = [int(x) for x in input().strip().split()]
|
||||||
adjlist[l[0]].append([ l[1], l[2] ])
|
adjlist[l[0]].append([ l[1], l[2] ])
|
||||||
adjlist[l[1]].append([ l[0], l[2] ])
|
adjlist[l[1]].append([ l[0], l[2] ])
|
||||||
print(PrimsAlgorithm(adjlist))
|
print(PrimsAlgorithm(adjlist))
|
||||||
|
@ -263,4 +263,7 @@ def multi_a_star(start, goal, n_hueristic):
|
|||||||
print()
|
print()
|
||||||
print("# is an obstacle")
|
print("# is an obstacle")
|
||||||
print("- is the path taken by algorithm")
|
print("- is the path taken by algorithm")
|
||||||
multi_a_star(start, goal, n_hueristic)
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
multi_a_star(start, goal, n_hueristic)
|
||||||
|
@ -1,19 +1,5 @@
|
|||||||
from __future__ import print_function
|
from __future__ import print_function
|
||||||
# n - no of nodes, m - no of edges
|
|
||||||
n, m = list(map(int,input().split()))
|
|
||||||
|
|
||||||
g = [[] for i in range(n)] #graph
|
|
||||||
r = [[] for i in range(n)] #reversed graph
|
|
||||||
# input graph data (edges)
|
|
||||||
for i in range(m):
|
|
||||||
u, v = list(map(int,input().split()))
|
|
||||||
g[u].append(v)
|
|
||||||
r[v].append(u)
|
|
||||||
|
|
||||||
stack = []
|
|
||||||
visit = [False]*n
|
|
||||||
scc = []
|
|
||||||
component = []
|
|
||||||
|
|
||||||
def dfs(u):
|
def dfs(u):
|
||||||
global g, r, scc, component, visit, stack
|
global g, r, scc, component, visit, stack
|
||||||
@ -43,4 +29,21 @@ def kosaraju():
|
|||||||
scc.append(component)
|
scc.append(component)
|
||||||
return scc
|
return scc
|
||||||
|
|
||||||
print(kosaraju())
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
# n - no of nodes, m - no of edges
|
||||||
|
n, m = list(map(int,input().strip().split()))
|
||||||
|
|
||||||
|
g = [[] for i in range(n)] #graph
|
||||||
|
r = [[] for i in range(n)] #reversed graph
|
||||||
|
# input graph data (edges)
|
||||||
|
for i in range(m):
|
||||||
|
u, v = list(map(int,input().strip().split()))
|
||||||
|
g[u].append(v)
|
||||||
|
r[v].append(u)
|
||||||
|
|
||||||
|
stack = []
|
||||||
|
visit = [False]*n
|
||||||
|
scc = []
|
||||||
|
component = []
|
||||||
|
print(kosaraju())
|
||||||
|
Loading…
x
Reference in New Issue
Block a user