def main(args): """Reads in a social network from a file, and then repeatedly reads in individuals from standard input and prints out their degrees of separation. Takes three command-line arguments: the name of a file, a delimiter, and the name of the distinguished individual. Each line in the file contains the name of a vertex, followed by a list of the names of the vertices adjacent to that vertex, separated by the delimiter. :param args: the command-line arguments """ filename = args[1] delimiter = args[2] source = args[3] sg = SymbolGraph(filename, delimiter) G = sg.graph() if not sg.contains(source): stdio.writeln("{} not in database".format(source)) return s = sg.index_of(source) bfs = BreadthFirstPaths(G, s) while not stdio.isEmpty(): sink = stdio.readLine() if sg.contains(sink): t = sg.index_of(sink) if bfs.has_path_to(t): for v in bfs.path_to(t): stdio.writef("\t%s\n", sg.name_of(v)) else: stdio.writeln("\tNot connected") else: stdio.writeln("\tNot in database.")
""" self._validateVertex(v) return self._keys[v] def graph(self): return self._graph def _validateVertex(self, v): # throw an IllegalArgumentException unless 0 <= v < V V = self._graph.V() if v < 0 or v >= V: raise ValueError("vertex {} is not between 0 and {}".format(v, V - 1)) if __name__ == "__main__": import sys filename = sys.argv[1] delimiter = sys.argv[2] sg = SymbolGraph(filename, delimiter) graph = sg.graph() while stdio.hasNextLine(): source = stdio.readLine() if sg.contains(source): s = sg.index_of(source) for v in graph.adj(s): stdio.writef("\t%s\n", sg.name_of(v)) else: stdio.writef("input not contain '%i'", source)
file = open(filename, "r") st = {} ts = {} line = file.readline() while line: fields = line.split(separator) key = fields[0] for i in range(1, len(fields)): val = fields[i] if key not in st: st[key] = Queue() if val not in ts: ts[val] = Queue() st.get(key).enqueue(val) ts.get(val).enqueue(key) line = file.readline() print("Done indexing") # read queries from standard input, one per line while not stdio.isEmpty(): query = stdio.readLine() if query in st: for vals in st.get(query): print(" " + vals) if query in ts: for keys in ts.get(query): print(" " + keys) file.close()