Delete a node from a digraph and record number of arcs removed 30 Marks For each…
- User
Delete a node from a digraph and record number of arcs removed 30 Marks For each digraphs in a set of digraphs given as adjacency lists, read in the digraph, delete the node with index n − 3 where n is the order of the digraph, and write the digraph back to the terminal. After the adjacency lists, write out how many arcs have been removed in the process. Assume input digraphs have order at least 3. Input format: described below under the heading “Digraph input format”. Output format: the same as the input format but with one extra line after each digraph stating the number of arcs that were removed when the node was removed. Ensure that you maintain the node naming conventions. For the example input shown below, the first digraph would have node with index 1 removed, and the second graph would have node index 0 removed, so the output would be 3 2 0 3 2 0 2 0 Here the first line indicates the order of the new digraph is 3, the next three lines are the three adjacency lists showing the arcs of the this digraph, and the 3 on line 5 indicates that three arcs were removed from the input digraph. The next line has a 2 indicating that the next digraph has order 2 and so on. Digraph input format A sequence of one or more digraphs is taken from the standard input (eg sys.stdin). Each graph is represented by an adjacency list. The first line is an integer n indicating the order of the graph. This is followed by n white space separated lists of adjacencies for nodes labeled 0 to n - 1. The lists are sorted. The input will be terminated by a line consisting of one zero (0). This line should not be processed. The sample input below shows two digraphs, the first has node set {0, 1, 2, 3} and arc set {(0, 1),(0, 3),(1, 2),(1, 3),(2, 0)}, the second has node set {0, 1, 2} and arc set {(0, 1),(0, 2),(2, 1)}. 4 1 3 2 3 0 3 1 2 1 0
- Assistant
Here's a Python code to delete a node from a digraph and record the number of arcs removed:
import sys def delete_node_and_count_arcs(digraph, node_to_delete): removed_arcs = 0 for node in digraph: if node_to_delete in digraph[node]: removed_arcs += 1 digraph[node].remove(node_to_delete) if node_to_delete in digraph: removed_arcs += len(digraph[node_to_delete]) del digraph[node_to_delete] return removed_arcs def read_digraph_from_input(lines): order = int(lines[0]) digraph = {i: list(map(int, line.split())) for i, line in enumerate(lines[1:order + 1])} return digraph def print_digraph_and_removed_arcs(digraph, removed_arcs): print(len(digraph)) for node in sorted(digraph.keys()): print(" ".join(map(str, digraph[node]))) print(removed_arcs) def main(): input_lines = [line.strip() for line in sys.stdin.readlines()] input_lines.pop() # Remove last "0" line i = 0 while i < len(input_lines): n = int(input_lines[i]) digraph = read_digraph_from_input(input_lines[i:i + n + 1]) node_to_delete = n - 3 removed_arcs = delete_node_and_count_arcs(digraph, node_to_delete) print_digraph_and_removed_arcs(digraph, removed_arcs) i += n + 1 if __name__ == "__main__": main() - User
Back and cross arcs in a DFS 30 Marks For a given set of digraphs, write a program that performs DFS on each digraph starting at node 0 and prints out the total number of back arcs and cross arcs resulting from the traversal. Use our standard convention that when there is a choice of white or grey nodes, the one with the lowest index should be chosen. Input format: described below under the heading, “Digraph input format”. Output format: For each input digraph, print out a line with the number of back arcs, then whitespace, and then the number of cross arcs. For the example input shown below, the output would be 1 0 0 1
Digraph input format A sequence of one or more digraphs is taken from the standard input (eg sys.stdin). Each graph is represented by an adjacency list. The first line is an integer n indicating the order of the graph. This is followed by n white space separated lists of adjacencies for nodes labeled 0 to n - 1. The lists are sorted. The input will be terminated by a line consisting of one zero (0). This line should not be processed. The sample input below shows two digraphs, the first has node set {0, 1, 2, 3} and arc set {(0, 1),(0, 3),(1, 2),(1, 3),(2, 0)}, the second has node set {0, 1, 2} and arc set {(0, 1),(0, 2),(2, 1)}. 4 1 3 2 3 0 3 1 2 1 0
- Assistant
Here's a Python code that performs DFS on each digraph starting at node 0 and prints out the total number of back arcs and cross arcs:
from collections import defaultdict def dfs(graph, node, visited, parent, depths): visited.add(node) depths[node] = depths[parent] + 1 if parent is not None else 0 back_arcs = 0 cross_arcs = 0 for neighbor in graph[node]: if neighbor not in visited: back, cross = dfs(graph, neighbor, visited, node, depths) back_arcs += back cross_arcs += cross elif depths[node] > depths[neighbor] and neighbor != parent: back_arcs += 1 elif depths[node] <= depths[neighbor] and neighbor != parent: cross_arcs += 1 return back_arcs, cross_arcs def read_digraph_from_input(lines): order = int(lines[0]) digraph = defaultdict(list) for i, adjacencies in enumerate(lines[1:order + 1]): digraph[i] = list(map(int, adjacencies.split())) return digraph def main(): input_lines = [line.strip() for line in sys.stdin.readlines()] input_lines.pop() # Remove last "0" line i = 0 while i < len(input_lines): n = int(input_lines[i]) digraph = read_digraph_from_input(input_lines[i:i + n + 1]) visited = set() depths = {node: None for node in digraph} back_arcs, cross_arcs = dfs(digraph, 0, visited, None, depths) print(back_arcs, cross_arcs) i += n + 1 if __name__ == "__main__": main()