Ed Chao
2 min readApr 21, 2020

--

Forward Star Data Structure in Python

Can’t print all origins and their arcs quantities, if don’t add one more blank row at end of original list!!

## read data from file
import pandas as pd
import numpy as np
pd.set_option('display.precision', 0)
pd.set_option('display.width', None)
path = 'C:/Users/Admin_TransLab/Documents/Ed_Chao/03_高等運輸規劃/utown/'
file1 = 'UTOWN.txt'
file2 = 'utownod.txt'
col_names1 = ['origin', 'destination', 'distance']
df = pd.read_csv(path + file2, delim_whitespace = True, header = None, names = col_names1, index_col = False)
origin_list = [] # origin list
count = 1 # counter
count_list = [1]
destination_list = [] # destination list
distance_list = [] # distance(cost) list
for i in range(len(df.origin)): # start to memory arc
count += 1
if df.origin[i] == 0:
break
elif df.origin[i] != df.origin[i+1]: # if != remember it
origin_list.append(df.origin[i])
count_list.append(count)
else:
count = count
for i in range(len(origin_list)):
print('b[%d] = %d' %(origin_list[i], count_list[i]))
##################################################################### enter values manually and use def function
def forward_star():
temp = [1] # store enter values temporarily
origin_list = [1] # origin list
count = 0 # counter
count_list = [1]
destination_list = [] # destination list
distance_list = [] # distance(cost) list
arc = 5 # set up arc number
for i in range(arc): # start to memory arc
origin= eval(input('enter the origin:'))
temp.append(origin)
count += 1
if origin == 0 : # for jump out
break
elif origin != temp[i]: # if != remember it
origin_list.append(origin)
count_list.append(count)
else:
count == count
destination_list.append(eval(input('enter the destination')))
distance_list.append(eval(input('enter the distance of this arc')))
count_list.append(count)
for i in range(len(origin_list)):
print('b[%d] = %d' %(origin_list[i], count_list[i]))
for j in range(len(destination_list)):
print('origin[%d] --->destination[%d] = %d' %(j+1, j+1, distance_list[j]))

forward_star()

--

--

Ed Chao

Playground for a old student. Records about learning, life and interesting stuff