if you want to remove an article from website contact us from top.

# consider an undirected graph with 20 vertices. what is the maximum number of edges that can be there in the graph such that the graph will remain disconnected? also, the self-loop and multiple edges are not allowed.

Category :

### Mohammed

Guys, does anyone know the answer?

get consider an undirected graph with 20 vertices. what is the maximum number of edges that can be there in the graph such that the graph will remain disconnected? also, the self-loop and multiple edges are not allowed. from screen.

## math

What is the maximum number of edges in a directed graph with n nodes? Is there any upper bound? ## What is the maximum number of edges in a directed graph with n nodes? [closed]

Asked 11 years, 5 months ago

Modified 8 months ago

Viewed 212k times 81

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.

This question does not appear to be about programming within the scope defined in the help center.

Closed last year.

Improve this question

What is the maximum number of edges in a directed graph with n nodes? Is there any upper bound?

math graph max Share

edited Jun 4, 2021 at 20:55

bitFlipper 4775 5 silver badges 20 20 bronze badges

asked Feb 20, 2011 at 16:44

Loolooii 8,11314 14 gold badges 62 62 silver badges 89 89 bronze badges

@LiorKogan what about the "algorithm" part? –

Loolooii

Jan 11, 2016 at 13:19

2

Sorry, but I don't see any "algorithm" part. Just a combinatorical question –

Lior Kogan

Jan 11, 2016 at 18:52

2

I'm voting to close this question as off-topic because its not about programming. –

Brian Tompsett - 汤莱恩

Aug 20, 2016 at 10:24

4

I’m voting to close this question because it is not specifically a programming problem. –

Nick stands with Ukraine

Jun 4, 2021 at 10:32

12 Answers Introducing: Trending sort

You can now choose to sort by Trending, which boosts votes that have happened recently, helping to surface more up-to-date answers.

Trending is based off of the highest score sort and falls back to it if no posts are trending.

99

If you have N nodes, there are N - 1 directed edges than can lead from it (going to every other node). Therefore, the maximum number of edges is N * (N - 1).

Share

answered Feb 20, 2011 at 16:49

Chris Smith 17.6k12 12 gold badges 57 57 silver badges 79 79 bronze badges 14

Correct. If edges are allowed to go from a node to itself, then the maximum is N^2. –

ypercubeᵀᴹ

Feb 22, 2011 at 23:56

3

@M.A you are correct if you are talking about an undirected graph. In a directed graph however edge (A,B) is not the same as edge (B,A) –

Bob9630

Feb 7, 2014 at 22:30

33

N*(N-1) is number of edges in directed graph. Number of edge in undirected graph is (N * (N-1)) / 2 –

Charles Chow

May 23, 2014 at 16:56

that is under the assumption that the graph is directed –

moldovean

Mar 21, 2015 at 20:49

1

Same thought as @ypercube, the answer is correct but does not consider self-loops in the directed graph. –

algarecu Oct 2, 2015 at 9:13

Show 2 more comments

48

## Directed graph:

Question: What's the maximum number of edges in a directed graph with n vertices?

Assume there are no self-loops.

Assume there there is at most one edge from a given start vertex to a given end vertex.

Each edge is specified by its start vertex and end vertex. There are n choices for the start vertex. Since there are no self-loops, there are n-1 choices for the end vertex. Multiplying these together counts all possible choices.

## Undirected graph

Question: What's the maximum number of edges in an undirected graph with n vertices?

Assume there are no self-loops.

Assume there there is at most one edge from a given start vertex to a given end vertex.

In an undirected graph, each edge is specified by its two endpoints and order doesn't matter. The number of edges is therefore the number of subsets of size 2 chosen from the set of vertices. Since the set of vertices has size n, the number of such subsets is given by the binomial coefficient C(n,2) (also known as "n choose 2"). Using the formula for binomial coefficients, C(n,2) = n(n-1)/2.

Share

answered Jan 10, 2016 at 9:10

d.danailov 9,2984 4 gold badges 49 49 silver badges 35 35 bronze badges Add a comment 28

In an undirected graph (excluding multigraphs), the answer is n*(n-1)/2. In a directed graph an edge may occur in both directions between two nodes, then the answer is n*(n-1).

Share

answered Jun 8, 2013 at 1:28

Leading Edge Boomer 2813 3 silver badges 2 2 bronze badges Add a comment 22

In addition to the intuitive explanation Chris Smith has provided, we can consider why this is the case from a different perspective: considering undirected graphs.

To see why in a DIRECTED graph the answer is n*(n-1), consider an undirected graph (which simply means that if there is a link between two nodes (A and B) then you can go in both ways: from A to B and from B to A). The maximum number of edges in an undirected graph is n(n-1)/2 and obviously in a directed graph there are twice as many.

Good, you might ask, but why are there a maximum of n(n-1)/2 edges in an undirected graph? For that, Consider n points (nodes) and ask how many edges can one make from the first point. Obviously, n-1 edges. Now how many edges can one draw from the second point, given that you connected the first point? Since the first and the second point are already connected, there are n-2 edges that can be done. And so on. So the sum of all edges is:

स्रोत : stackoverflow.com

## 4.1   Undirected Graphs

4.1   Undirected Graphs Graphs.

A graph is a set of vertices and a collection of edges that each connect a pair of vertices. We use the names 0 through V-1 for the vertices in a V-vertex graph. ## Glossary.

Here are some definitions that we use.

A self-loop is an edge that connects a vertex to itself.

Two edges are parallel if they connect the same pair of vertices.

When an edge connects two vertices, we say that the vertices are adjacent to one another and that the edge is incident on both vertices.

The degree of a vertex is the number of edges incident on it.

A subgraph is a subset of a graph's edges (and associated vertices) that constitutes a graph.

A path in a graph is a sequence of vertices connected by edges, with no repeated edges.

A simple path is a path with no repeated vertices.

A cycle is a path (with at least one edge) whose first and last vertices are the same.

A simple cycle is a cycle with no repeated vertices (other than the requisite repetition of the first and last vertices).

The length of a path or a cycle is its number of edges.

We say that one vertex is connected to another if there exists a path that contains both of them.

A graph is connected if there is a path from every vertex to every other vertex.

A graph that is not connected consists of a set of connected components, which are maximal connected subgraphs.

An acyclic graph is a graph with no cycles.

A tree is an acyclic connected graph.

A forest is a disjoint set of trees.

A spanning tree of a connected graph is a subgraph that contains all of that graph's vertices and is a single tree. A spanning forest of a graph is the union of the spanning trees of its connected components.

A bipartite graph is a graph whose vertices we can divide into two sets such that all edges connect a vertex in one set with a vertex in the other set.   ## Undirected graph data type.

We implement the following undirected graph API. The key method adj() allows client code to iterate through the vertices adjacent to a given vertex. Remarkably, we can build all of the algorithms that we consider in this section on the basic abstraction embodied in adj().

We prepare the test data tinyG.txt, mediumG.txt, and largeG.txt, using the following input file format. GraphClient.java contains typical graph-processing code.

## Graph representation.

We use the adjacency-lists representation, where we maintain a vertex-indexed array of lists of the vertices connected by an edge to each vertex. Graph.java implements the graph API using the adjacency-lists representation. AdjMatrixGraph.java implements the same API using the adjacency-matrix representation.

## Depth-first search.

Depth-first search is a classic recursive method for systematically examining each of the vertices and edges in a graph. To visit a vertex

Mark it as having been visited.

Visit (recursively) all the vertices that are adjacent to it and that have not yet been marked.

DepthFirstSearch.java implements this approach and the following API: ## Finding paths.

It is easy to modify depth-first search to not only determine whether there exists a path between two given vertices but to find such a path (if one exists). We seek to implement the following API:

To accomplish this, we remember the edge v-w that takes us to each vertex w for the first time by setting edgeTo[w] to v. In other words, v-w is the last edge on the known path from s to w. The result of the search is a tree rooted at the source; edgeTo[] is a parent-link representation of that tree. DepthFirstPaths.java implements this approach.

Depth-first search finds some path from a source vertex s to a target vertex v. We are often interested in finding the shortest such path (one with a minimal number of edges). Breadth-first search is a classic method based on this goal. To find a shortest path from s to v, we start at s and check for v among all the vertices that we can reach by following one edge, then we check for v among all the vertices that we can reach from s by following two edges, and so forth.

To implement this strategy, we maintain a queue of all vertices that have been marked but whose adjacency lists have not been checked. We put the source vertex on the queue, then perform the following steps until the queue is empty:

Remove the next vertex v from the queue.

स्रोत : algs4.cs.princeton.edu

## Maximum number of edges to be removed to contain exactly K connected components in the Graph

A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions. ## Maximum number of edges to be removed to contain exactly K connected components in the Graph

Difficulty Level : Hard

Last Updated : 27 Aug, 2021

Given an undirected graph G with N nodes, M edges, and an integer K, the task is to find the maximum count of edges that can be removed such that there remains exactly K connected components after the removal of edges. If the graph cannot contain K connect components, print -1.

Examples:Input: N = 4, M = 3, K = 2, Edges[][] = {{1, 2}, {2, 3}, {3, 4}} Output: 1Explanation:

One possible way is to remove edge [1, 2]. Then there will be 2 connect components as shown below: Input: N = 3, M = 3, K = 3, Edges[][] = {{1, 2}, {2, 3}, {3, 1}} Output: 3Explanation: All edges can be removed to make 3 connected components as shown below: Recommended: Please try your approach on first, before moving on to the solution.

Approach: To solve the given problem, count the number of connected components present in the given graph. Let the count be C. Observe that if C is greater than K then no possible edge removal can generate K connected components as the number of connected components will only increase. Otherwise, the answer will always exist.

Following observations need to be made in order to solve the problem:

Suppose C1, C2, …, Cc, are the number of node in each connected component. Then, each component must have edges as C1 – 1, C2 – 1, …, Cc -1 after edges are removed. Therefore,

C1 – 1 + C2 – 1 + … + Cc – 1 = C1 + C2 + … + Cc – C = N – C, where N is the number of nodes.

The above condition will give us the C connected components by removing M – (N – C) edges as N – C edges are needed to make C components. To get K components, (K – C) more edges must be removed.

Hence, the total count of edges to be removed is given by:

M – (N – C) + (K – C) = M – N + K

Follow the steps below to solve the problem:

Count the number of connected components present in the given graph. Let the count be C.

If C is greater than K, print -1.

Else print M – N + K where N is the number f nodes, M is the number of edges and K is the required number of connected components.

Below is the implementation of the above approach:

## C++

// C++ program for the above approach

#include

using namespace std;

map

class Graph { public: int V;

Graph(int);

void DFS(int, vector &);

} * g; // Constructor Graph::Graph(int V) { // No. of vertices this->V = V;

// Dictionary of lists

for(int i = 1; i <= V; i++)

}

// Function to add edge

// in the graph

void Graph::addEdge(int v, int w)

{

}

// Function to perform DFS

void Graph::DFS(int s, vector &visited)

{

// Create a stack for DFS

stack stack;

// Push the current source node

stack.push(s);

while (!stack.empty())

{

// Pop a vertex from stack

// and print it s = stack.top(); stack.pop();

// Traverse adjacent vertices

// of the popped vertex s

for(auto node : adj[s])

{ if (!visited[node]) {

// If adjacent is unvisited,

// push it to the stack

visited[node] = true;

stack.push(node); } } } }

// Function to return the count

// edges removed

void countRemovedEdges(int N, int M, int K)

{ int C = 0;

// Initially mark all vertices

vector

// as not visited

visited(g->V + 1, false);

for(int node = 1; node <= N; node++)

{

// If node is unvisited

if (!visited[node]) {

// Increment Connected

// component count by 1

C = C + 1;

// Perform DFS Traversal

g->DFS(node, visited);

// Print the result if (C <= K)

cout << M - N + K << endl;

else cout << -1 << endl; } } } // Driver Code

int main(int argc, char const *argv[])

{

int N = 4, M = 3, K = 2;

// Create Graph g = new Graph(N); // Given Edges

स्रोत : www.geeksforgeeks.org

Do you want to see answer or more ?
Mohammed 3 month ago

Guys, does anyone know the answer?