Question
#!/usr/bin/env python3 import sys import os.path def read_integer_graph(gname): //this function works, no need help, it displays result = [] with open(gname + '.ig') as f:
#!/usr/bin/env python3
import sys import os.path
def read_integer_graph(gname): //this function works, no need help, it displays result = [] with open(gname + '.ig') as f: lines = f.readline() lines = f.readline() for rig in f: rig.split() x , y = rig.split() result.append([x,y]) return result
def read_string_graph(gname): //this function needs help, it compiles but didn't work, not displaying at all result = [] with open(gname + '.sg') as f: for rsg in f: rsg.split() x , y = rsg.split() result.append([x,y]) return result
def write_dot_graph(gname,g): //this function needs help,my code doesn't work,not displaying like the below filename.dot graph
with open(gname + '.dot') as g: print('graph ' + gname + '{') a = set() for v in g: for w in a: if not w in v: print('\"' + 'v' + '\"--\"' + 'w' + '\"') g.append(v) print('}')
def usage(): print('usage: ./graph.py [ file.ig | file.sg sep ]') exit(0)
def main(): if len(sys.argv) < 2: usage() f = sys.argv[1] gname,kind = os.path.splitext(f) if kind == '.ig': if len(sys.argv) != 2: usage() g = read_integer_graph(gname) elif kind == '.sg': if len(sys.argv) != 3: usage() g = read_string_graph(gname,sys.argv[2]) else: usage() write_dot_graph(gname,g) if __name__ == '__main__': main()
//this is how the the filename.ig and filename.sg and filename.dot looks like
filename.ig
13 13 0 5 4 3 0 1 9 12 6 4 5 4 0 2 11 12 9 10 0 6 7 8 9 11 5 3
filename.sg
JFK MCO ORD DEN ORD HOU DFW PHX JFK ATL ORD DFW ORD PHX ATL HOU DEN PHX PHX LAX JFK ORD DEN LAS DFW HOU ORD ATL LAS LAX ATL MCO HOU MCO LAS PHX STL PDX
//this is how filename.dot graph should look like
graph routes {"JFK" -- "ATL""JFK" -- "MCO""JFK" -- "ORD""MCO" -- "HOU""MCO" -- "ATL""ORD" -- "HOU""ORD" -- "ATL""ORD" -- "DEN""ORD" -- "PHX""ORD" -- "DFW""DEN" -- "PHX""DEN" -- "LAS""HOU" -- "ATL""HOU" -- "DFW""DFW" -- "PHX""PHX" -- "LAX""PHX" -- "LAS""LAX" -- "LAS""STL" -- "PDX"}
//this is in Java for the function write dot graph
public static void toDot(Graph g, String gname) throws Exception { java.io.PrintStream out = new java.io.PrintStream(gname + ".dot"); out.println("graph " + gname + " {"); // challenge is to avoid drawing every edge twice Set visited = new HashSet(); for (V v : g.vertices()) { for (V w : g.neighbors(v)) if (!visited.contains(w)) out.println("\"" + v + "\" -- \"" + w + "\""); visited.add(v); } out.println("}"); out.close(); }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started