Given an adjacency list representation for an undirected graph. Write a function to count the number of edges in the undirected graph.
I know the number of edges in an undirected graph is n(n-1)/2 but I don't know how to write a function for that.
Considering that I've a list and using that I'll be counting the number of edges. How should I start?
n(n-1)/2 is the maximum number of edges in a simple undirected graph, not the number of edges for every such graph.
Given that you have an adjacency list representation, let it be the case that vertices u and v have an edge between them. Then, v will appear in the adjacency list of u and u will appear in the adjacency list of v. This is true for any u and v.
Hence, if you count the total number of entries of all the elements in the adjacency list of each vertex, the result will be twice the number of edges in the graph. Dividing this value by two will give the desired result.
I know the number of edges in an undirected graph is n(n-1)/2 but I don't know how to write a function for that.
The maximum number of edges in undirected graph is n(n-1)/2.
not only number of edges.
Suppose the povided array is.
0[1,2,3]
1[0,2]
2[0,1,4]
3[0]
4[2]
then number of edges will be. #java
for (ArrayList<Integer> verts : adj) {
sumOfVert += verts.size();
}
return sumOfVert/2;// Output will be 5;
Related
I am studying algorithms, and I have seen an exercise like this
I can overcome this problem with exponential time but. I don't know how to prove this linear time O(E+V)
I will appreciate any help.
Let G be the graph where the minimum spanning tree T is embedded; let A and B be the two trees remaining after (u,v) is removed from T.
Premise P: Select minimum weight edge (x,y) from G - (u,v) that reconnects A and B. Then T' = A + B + (x,y) is a MST of G - (u,v).
Proof of P: It's obvious that T' is a tree. Suppose it were not minimum. Then there would be a MST - call it M - of smaller weight. And either M contains (x,y), or it doesn't.
If M contains (x,y), then it must have the form A' + B' + (x,y) where A' and B' are minimum weight trees that span the same vertices as A and B. These can't have weight smaller than A and B, otherwise T would not have been an MST. So M is not smaller than T' after all, a contradiction; M can't exist.
If M does not contain (x,y), then there is some other path P from x to y in M. One or more edges of P pass from a vertex in A to another in B. Call such an edge c. Now, c has weight at least that of (x,y), else we would have picked it instead of (x,y) to form T'. Note P+(x,y) is a cycle. Consequently, M - c + (x,y) is also a spanning tree. If c were of greater weight than (x,y) then this new tree would have smaller weight than M. This contradicts the assumption that M is a MST. Again M can't exist.
Since in either case, M can't exist, T' must be a MST. QED
Algorithm
Traverse A and color all its vertices Red. Similarly label B's vertices Blue. Now traverse the edge list of G - (u,v) to find a minimum weight edge connecting a Red vertex with a Blue. The new MST is this edge plus A and B.
When you remove one of the edges then the MST breaks into two parts, lets call them a and b, so what you can do is iterate over all vertices from the part a and look for all adjacent edges, if any of the edges forms a link between the part a and part b you have found the new MST.
Pseudocode :
for(all vertices in part a){
u = current vertex;
for(all adjacent edges of u){
v = adjacent vertex of u for the current edge
if(u and v belong to different part of the MST) found new MST;
}
}
Complexity is O(V + E)
Note : You can keep a simple array to check if vertex is in part a of the MST or part b.
Also note that in order to get the O(V + E) complexity, you need to have an adjacency list representation of the graph.
Let's say you have graph G' after removing the edge. G' consists have two connected components.
Let each node in the graph have a componentID. Set the componentID for all the nodes based on which component they belong to. This can be done with a simple BFS for example on G'. This is an O(V) operation as G' only has V nodes and V-2 edges.
Once all the nodes have been flagged, iterate over all unused edges and find the one with the least weight that connects the two components (componentIDs of the two nodes will be different). This is an O(E) operation.
Thus the total runtime is O(V+E).
I am required to use a greedy algorithm to resolve this problem (here m is the number of edges and n is the number of vertices in the original graph). Intuitively, I know it is somehow about density of graph (due to m/n*(n-1) part), so I try to use greedy algorithm to remove the vertex with minimum degree each iteration until I get a k nodes graph, but I don't know how can I GARUNTEE the algorithm give me the final graph with at least m*k*(k-1)/n*(n-1) edges.
Looking for any hints, Thanks.
Let's define the density of a graph p=m/(n*(n-1)) (for undirected graph it should be p=2*m/(n*(n-1)). Note that for a graph with density p and k vertices, it has (k*(k-1)) * p edges. Also note that when you greedily remove a vertex with the minimum degree, it won't decrease the density of the graph (will proof it below). So when you get the subgraph with k vertices, its density is equal to or greater than m/(n*(n-1)), then the subgraph contains at least k*(k-1)(m*/n*(n-1)) edges.
Let G be a graph with m edges and n vertices, then p=m/n. Let d the minimum degree of all vertices. then we have n * d <= m i.e., d <= m/n. So when you remove the vertex with the minimum degree, you get a graph with m-d edges and n-1 vertices, then the density of the new graph will be p'=(m-d)/(n-1) >= (m-m/n)/(n-1) = m/n = p. So we have that the greedy algorithm won't decrease the density of a graph.
Imagine you have a weighted undirected graph G=(V,E) that is fully connected (edge between every pair of vertices). You seek to have a graph G' where G' is a subset of G where vertices with their corresponding edges have been removed such that the edge with the minimum weight is d.
Trivially you can just remove all vertices but a few with highly weighted edges. But what if you want G' to be as big as possible ? What is the way to remove the minimal set of vertices to satisfy the weight condition ?
For instance I have graph A,B,C (A,B) = 2 (A,C) = 3 and (B,C) = 2 and my minimum d=2. I could remove both A and C to get just B. But I could also remove B to leave A and C. The second solution is the one with the minimal amount of removals.
I need a method to find root of a directed acyclic graph.I am using boolean adjancency matix to represent graph in java.so please suggest.Also graph is unweighted graph
Just find the node where indegree is 0. For below algorithm to work we assume that none of nodes in graph are isolated.
int indegree[N]={0};
for(i=0;i<n;++i){
for(j=0;j<n;++j){
if(graph[i][j]==1){ //assuming edge from i to j
indegree[j]++;
}
}
}
for(int i=0;i<n;++i){
if(indegree[i]==0) add i to roots;
}
You are looking for nodes with no in-edges. If the adjacency matrix is encoded so that entry (i,j) contains a 1 if and only if there is an edge from i to j, then for node K to be a root, there must be no edges of the form i->K, therefore no 1's in entries of the form (i, K). So you are looking for columns K with all zeros. Each such column is a root.
In pseudocode,
roots = {}
for k in 1 to N
for i in 1 to N
if adjacencies[i, k] > 0
continue with next k value
add k to roots
It can be done in linear time. It is basically doing DFS over the graph with all the edges reversed.
Pick up any vertex in the given graph G
Check if the vertex has in-degree equal to 0. If it does we have found a vertex which is root of the graph.
If not then, mark the current vertex v as visited and repeat the same process over all the unvisited parents of v.
This will fetch all the required vertices with in-degree equal to zero or roots of a DAG.
Given an adjacency-list representation of a directed graph, how long does it take
to compute the out-degree of every vertex? How long does it take to compute the
in-degrees?
Thanks
Adjacency-list representation of a directed graph:
Out-degree of each vertex
Graph out-degree of a vertex u is equal to the length of Adj[u].
The sum of the lengths of all the adjacency lists in Adj is |E|.
Thus the time to compute the out-degree of every vertex is Θ(V + E)
In-degree of each vertex
The in-degree of a vertex u is equal to the number of times it appears in all the lists in Adj.
If we search all the lists for each vertex, time to compute the in-degree of every vertex is Θ(VE)
Alternatively, we can allocate an array T of size |V| and initialize its entries to zero.
We only need to scan the lists in Adj once, incrementing T[u] when we see 'u' in the lists.
The values in T will be the in-degrees of every vertex.
This can be done in Θ(V + E) time with Θ(V) additional storage.
Both are O(m + n) where m is the number of edges and n is the number of vertices.
Start a set of counters, one for each vertex, one for in-degree and out for out-degree.
Scan the edges. For the out vertex of each edge, add one to the out-degree counter for that vertex. For the in vertex of each edge, add one to the in-degree counter for that vertex. This is O(m) operation.
Output the out-degree and in-degree counters for each vertex, which is O(n).
That's how you get O(m + n).
out-degree for every vertex:theta(E)
in-degree for each vertex:O(E)
E is the number of edges of the graph
Since, its a directed graph and only the adjacency list is given.
The time taken to count the number of out-degrees would be theta (M+N) where M is the number of vertices and N refers to number of edges.
Whereas for the count of number of in-degrees, for any node you have to count the number of occurrences of that node in all other(rest of vertices) adjacency list. So, it would take theta(MN).
However, if you maintain an Array of size M, then you can do the counting of the in-degree in theta(M+N) with an additional space storage of theta(M)
Computing both the in-degree and out-degree takes theta(m + n) for a graph with m vertices and n edges. The reason that it is theta(m+n) and not O(m + n) because whatever may be the graph , it has to go through every vertex m and every edge n.
Given an adjacency-list representation Adj of a directed graph, the out-degree of a vertex u is equal to the length of Adj[u],
and the sum of the lengths of all the adjacency lists in Adj is |E|. Thus the time to compute the out-degree of every vertex is Θ(|V| + |E|).
The in-degree of a vertex u is equal to the number of times it appears in all the lists in Adj. If we search all the lists for each
vertex, the time to compute the in-degree of every vertex is Θ(|V|.|E|).
(Alternatively, we can allocate an array T of size |V| and initialize its entries to zero. Then we only need to scan the lists in
Adj once, incrementing T[u] when we see u in the lists. The values in T will be the in-degrees of every vertex. This can be
done in Θ(|V| + |E|) time with Θ(|V|) additional storage.)