Let N be the number of vertices and E be the edges in given directed graph.
For finding out-degree of any vertex we can simply traverse over its adjacency list and count the number of vertices present there. Therefore, the total time taken to compute out-degree of every vertex would be O(N+E) with O(1) space.
For finding in-degree of any vertex, we need to count the number of occurrences of that vertex by traversing over the adjacency lists of all the vertices. This will take time O(N+E) for each vertex, i.e. overall time complexity is O(N*(N+E)) with constant space. We can improve this by taking a Count array of size N, initialized with zero. Then traverse over the adjacency lists of all vertices just once and increment the counter of the respective vertices present there. So total time taken is O(N+E) with O(N) space.
Please ask for clarifications if needed any and kindly give feedback!
22.1-1 Given an adjacency-list representation of a directed graph, how long does it take to compute...
Exercise (15 points) Consider an adjacency-list representation of a directed graph G=(V.E). a) Propose in pseudocode an algorithm A to compute the in-degree of each vertex in V. b) What is the time complexity of A? c) Propose in pseudocode an algorithm B to compute the out-degree of each vertex in V. d) What is the time complexity of B?
For a directed graph the in-degree of a vertex is the number of edges it has coming in to it, and the out- degree is the number of edges it has coming out. (a) Let G[i,j] be the adjacency matrix representation of a directed graph, write pseudocode (in the same detail as the text book) to compute the in-degree and out-degree of every vertex in the Page 1 of 2 CSC 375 Homework 3 Spring 2020 directed graph. Store results...
Assume that the adjacency list representing a directed graph G has already been constructed. Show how to determine whether G contains a universal sink, that is a vertex with in-degree |V | − 1 and out-degree 0, in time O(|V |). **Please explain in detail
Consider the following directed graph, which is given in adjacency list form and where vertexes have numerical labels: 1: 2, 4, 6 2: 4, 5 3: 1, 2, 6, 9 4: 5 5: 4, 7 6: 1, 5, 7 7: 3, 5 8: 2, 6, 7 9: 1, 7 The first line indicates that the graph contains a directed edge from vertex 1 to vertex 2, from 1 to vertex 4, and 1 to 6, and likewise for subsequent lines....
5. The in-degree of a vertex in a directed graph is the number of edges directed into it. Here is an algorithm for labeling each vertex with its in-degree, given an adjacency-list representation of the graph. for each vertex i: i.indegree = 0 for each vertex i: for each neighbor j of i: j.indegree = j.indegree + 1 Label each line with a big-bound on the time spent at the line over the entire run on the graph. Assume that...
8, (10 pts) Show that given a directed graph G = (V,E) already stored in adjacency matrix form, determining if there is a vertex with in-degree n - 1 and out-degree 0 can be done in O(n) time where n is the number of vertices in V. 8, (10 pts) Show that given a directed graph G = (V,E) already stored in adjacency matrix form, determining if there is a vertex with in-degree n - 1 and out-degree 0 can...
Need help, thanks! Which of the following is false Adjacency list representation is preferred over adjacency matrix if the graph is sparse. The storage requirement of adjacency matrix is O(N^2) Accessing the weight of a specific link in adjacency list representation can take O(N) time O The weight of a link in adjacency list representation can be changed in 0(1) time. O All of the above are true
Lab 11 Adjacency Matrix Graph Objective: Create a class which constructs an adjacency matrix representation of a graph and performs a few graph operations. Write an Adjacency Matrix Graph class which has the following: Two constructors: Default which makes the matrix of a pre-defined size Parameterized which takes in a non-negative or 0 size and creates an empty matrix addEdge: this method returns nothing and takes in two string parameters and a weight. The two integer parameters correspond to the...
from collections import defaultdict # This class represents a directed graph using # adjacency list representation class Graph: # Constructor def __init__(self): # default dictionary to store graph self.graph = defaultdict(list) # function to add an edge to graph def addEdge(self,u,v): self.graph[u].append(v) # Function to print a BFS of graph def BFS(self, s): # Mark all the vertices as not visited visited = [False] * (len(self.graph)) # Create a queue for BFS queue...
1. Consider the directed graph on the right side of the following page and complete the exercises below. When conducting a search, be very careful (since a small error early on can result in a large deduction of marks), and whenever you have a "choice" of which adjacent vertex to consider, you must consider the vertices in numerical order from least to greatest. (10 marks total) a. Provide an adjacency list representation of this graph. b. Compute the depth-first search...