import numpy as np
def read_graph_file(filename):
    gfile = open(filename, 'r')
    content = gfile.readlines()
    n = int(content[0])
    adj=np.zeros((n,n))
    for i in range(1,len(content)):
        row = content[i].split()
        for j in range(len(row)):
            adj[i-1][j]= int(row[j])
    return n,adj
def read_graph():
    n = int(input("Enter the number of nodes"))
    adj=np.zeros((n,n))
    for i in range(n):
        for j in range(n):
            if (i !=j):
                adj[i][j] = int(input("Enter the weight of edge from node " + str(i) + " to "+str(j)))
    for i in range(n):
        for j in range(n):
            if (adj[i][j]==0):
                adj[i][j] = 9999
    return n, adj

        

def bellman(s):
    dist=np.array(n*[9999])
    dist[s]=0
    for i in range(n):
        for u in range(n):
            for v in range(n):
                if (adj[u][v] <9999) and (dist[u]+adj[u][v] < dist[v]):
                    dist[v]=dist[u]+adj[u][v]
    print("The distance from vertex ", s , " to all other vertices are:", dist)

        

"""
The adjacency matrix is stored in a file "adj_matrix.txt" which is a graph that
has 5 vertices. The contents of the sample file is
5
9999 6 9999 7 9999
9999 9999 5 8 -4
9999 -2 9999 9999 9999
9999 9999 -3 9999 9
2 9999 7 9999 9999
"""

n, adj = read_graph_file('adj_matrix.txt')
source = int(input("Enter start node"))
bellman(source)
read_graph_file()
