Relationship between vertices and edges in directed graph - algorithm

Why a complete, directed graph G on n vertices and m edges has m = n(n-1) edges. But I tried lots of examples showing this statement is false, which would be n(n-1)/2 But our professor gives true to this statement. Can someone explain to me the correctness of this statement?

I think you haven't completely understood the difference between directed and undirected graphs.
Remember in an undirected graph, the orientation of the edges doesn't matter.
But in directed graphs, the orientation of edges matter.
As an analogy, suppose the two cities, A and B are represented by two nodes of a graph, and the path joining them represents the edge. Now if the edge is un-directed, you can go from A to B or vice versa. However if it's directed, it means that it's a one way road, you can only go from A to B or vice versa (depending on the orientation).
Now to answer your question, in an undirected graph, the total number of edges would be
C(n,2) = (n*(n-1))/2.
But in a directed graph of n nodes, every edge can be doubled up. i.e, One from A to B and other from B to A.
Hence, the total number of edges = 2*C(n,2)
Which translates to n*(n-1).

You are talking about directed graph, so between two nodes A and B, you can have one directed edge from A to B and one from B to A.
Knowing that, you can find the expected result by induction.

Related

Algorithm for dividing graph into edge pairs

I've received a task to find an algorithm which divides a graph G(V,E) into pairs of neighboring edges (colors the graph, such that every pair of neighboring edges has the same color).
I've tried to tackle this problem by drawing out some random graphs and came to a few conclusions:
If a vertex is connected to 2(4,6,8...) vertices of degree 1, these make a pair of edges.
If a vertex of degree 1 is directly connected to a cycle, it doesn't matter which edge of the cycle is paired with the lone edge.
However, I couldn't come up with any other conclusions, so I tried a different approach. I thought about using DFS, finding articulation points and dividing graph into subgraphs with an even number of edges, because those should be dividable by this rule as well, and so on until I end up with only subgraphs of |E(G')| = 2.
Another thing I've come up with is to create a graph G', where E(G) = V(G') and V(G) = E(G'). That way I could get a graph, where I could remove pairs of vertices (former edges) either via DFS or always starting with leaf vertices along with their adjacent vertices.
The last technique is most appealing to me, but it seems to be the slowest one. Any feedback or tips on which of these methods would be the best is much appreciated.
EDIT: In other words, imagine the graph as a layout of a town. Vertices being crossroads, edges being the roads. We want to decorate (sweep, color) each road exactly once, but we can only decorate two connected roads at the same time. I hope this helps for clarification.
For example, having graph G with E={ab,bd,cd,ac,ae,be,bf,fd}, one of possible pair combinations is P={{ab,bf},{ac,cd},{ae,eb},{bd,df}}.
One approach is to construct a new graph G where:
A vertex in G corresponds to an edge in the original graph
An edge in G connects vertices a and b in G where a and b represent edges in the original graph that meet at a vertex in the original graph
Then, if I have understood the original problem correctly, the objective for G is to find the maximum matching, which can be done, for example, with the Blossom algorithm.

If there is a weight 3 from A->B and a weight 1 from B->A, does this mean that there are automatically 2 edges between A and B?

Regarding weighted graphs:
If there is a weight 3 from A->B and a weight 1 from B->A, does this mean that there are 2 edges between A and B? I'm 95% sure that the answer is yes, but I'd like to be certain. I'm trying to see if directed graphs with weight schemes like this are automatically multigraphs.
Thanks for your valuable input!
Marcus
As noted here
A graph with one edge from A to B and one edge from B to A, will simply be called a directed graph, but if we would have a directed graph with multiple edges from A to B, it will be called as multigraph or specifically, in our case, multidigraph.
Like, in the diagram(taken from same link).
In the first graph, there is no case when we have two edges from same origin and destination, so its a normal directed graph. But in case of second graph, there are two edges from e to d and from b to c. Thus, making it a multidigraph.
There are 2 edges between A and B, but they are not the same edge in a directed graph. There is the edge from A to B (A->B) and the edge from B to A (B->A). This does not make the graph a multigraph because those are two distinct edges.
In an undirected multigraph the source and destination nodes do not matter. The edges would no longer be (A->B) and (B->A) . They would simply each become (A, B) indicating an edge between A and B exists. If more than one edge connects any two nodes in an undirected graph, the graph becomes a multigraph.
A Direcited Multigraph must have multiple edges with the same source and destination. If there were multiple edges from A to B, then it would be a directed multigraph. However, you list two distinct directed edges. (A->B) and (B->A). These edges are not identical and so the graph you describe is not a multigraph.

Minimum Spanning Tree with only 2 equal edges

Assume you have an undirected-weighted graph G, with different edges weighs but for only two edges: w(e1)=w(e2)
I have to prove that G has at most one minimum spanning tree which includes e1.
Also I have to prove that G has at most one minimum spanning tree which doesnt include e1.
I only need a solution for the first one and will solve the second one alone.
Thanks
For solving part 1:
Consider the graph you get by removing e1 from G (and possibly one of it's vertices, if it's now not connected to the rest of the graph), let's call it G'.
In this graph (G') , all the edge weights are different.
Now suppose G has more than 1 MST which includes e1 - they would both be different MSTs for G'.
Now the trick is that there's a theorem that in this kind of graph (all edges are different), the MST is unique. see the proof(s) here.
edit: You can probably just take the proof from the link and edit it slightly for your case.

What is the difference between a directed and undirected graph

What is the difference between these fundamental types?
In drawings I see that the directed has arrows, but what exactly is meant by these arrows in the directed graph and the lack thereof in the undirected graph?
It means exactly what it sounds like. In a directed graph, direction matters. i.e. edge 2->3 means that edge is directed. There is only an edge from 2 to 3 and no edge from 3 to 2. Therefore you can go from vertex 2 to vertex 3 but not from 3 to 2.
In undirected graph 2-3 means the edge has no direction, i.e. 2-3 means you can go both from 2 to 3 and 3 to 2.
Note that in the representation of your graph, if you are using an adjacency matrix, directed 2->3 means adj[2][3]=true but adj[3][2]=false. In undirected it means adj[2][3]=adj[3][2]=true.
The difference is the same as between one directional and bidirectional streets - in directed graph, the direction matters and you can't use the edge in the other direction. An undirected graph can be simulated using a directed graph by using pairs of edges in both directions.
All of the answers so far are right.
Typically, a graph is depicted in diagrammatic form as a set of dots for the vertices, joined by lines or curves for the edges. The edges may be directed (asymmetric) or undirected (symmetric).
Imagine if the vertices represent people at a party. If there is an edge between the two people if they shake hands, then this is an undirected graph, because if person A shook hands with person B, then person B also shook hands with person A.
On the other hand, if the vertices represent people at a party, and there is an edge from person A to person B when person A knows of person B, then this graph is directed, because knowing of someone is not necessarily a symmetric relation.
Imagine graphs as a set of pumps( the circles) which can send liquid to others when are connected.In directed graphs the arrow show from where it comes and where the liquid (data) goes and in undirected graph it goes from both ways.Also a directed graph can have multiple arrows between two vertices(the pumps ) depending always on the graph.
A graph in which every edge is directed is called a Directed graph, and a graph in which every edge is undirected is called undirected graph.
In a directed graph, there is direction but in un-directed graph there is no direction.
Think in in terms of city network , where City A-> City B represents one way from City A to City B which means you can travel from City A to City B (may be through this path). It's an example of directed graph City c - City D represents the un-directed graph where you can travel in any direction
A directed graph is a graph in which edges have orientation (given by the arrowhead). This means that an edge (u, v) is not identical to edge (v, u).
An example could be nodes representing people and edges as a gift from one person to another.
An undirected graph is a graph in which edges don't have orientation (no arrowhead). This means that an edge (u, v) is identical to edge (v, u).
An example for this type of graph could be nodes representing cities and edges representing roads between cities.

Graph Has Two / Three Different Minimal Spanning Trees ?

I'm trying to find an efficient method of detecting whether a given graph G has two different minimal spanning trees. I'm also trying to find a method to check whether it has 3 different minimal spanning trees. The naive solution that I've though about is running Kruskal's algorithm once and finding the total weight of the minimal spanning tree. Later , removing an edge from the graph and running Kruskal's algorithm again and checking if the weight of the new tree is the weight of the original minimal spanning tree , and so for each edge in the graph. The runtime is O(|V||E|log|V|) which is not good at all, and I think there's a better way to do it.
Any suggestion would be helpful,
thanks in advance
You can modify Kruskal's algorithm to do this.
First, sort the edges by weight. Then, for each weight in ascending order, filter out all irrelevant edges. The relevant edges form a graph on the connected components of the minimum-spanning-forest-so-far. You can count the number of spanning trees in this graph. Take the product over all weights and you've counted the total number of minimum spanning trees in the graph.
You recover the same running time as Kruskal's algorithm if you only care about the one-tree, two-trees, and three-or-more-trees cases. I think you wind up doing a determinant calculation or something to enumerate spanning trees in general, so you likely wind up with an O(MM(n)) worst-case in general.
Suppose you have a MST T0 of a graph. Now, if we can get another MST T1, it must have at least one edge E different from the original MST. Throw away E from T1, now the graph is separated into two components. However, in T0, these two components must be connected, so there will be another edge across this two components that has exactly the same weight as E (or we could substitute the one with more weight with the other one and get a smaller ST). This means substitute this other edge with E will give you another MST.
What this implies is if there are more than one MSTs, we can always change just a single edge from a MST and get another MST. So if you are checking for each edge, try to substitute the edge with the ones with the same weight and if you get another ST it is a MST, you will get a faster algorithm.
Suppose G is a graph with n vertices and m edges; that the weight of any edge e is W(e); and that P is a minimal-weight spanning tree on G, weighing Cost(W,P).
Let δ = minimal positive difference between any two edge weights. (If all the edge weights are the same, then δ is indeterminate; but in this case, any ST is an MST so it doesn't matter.) Take ε such that δ > n·ε > 0.
Create a new weight function U() with U(e)=W(e)+ε when e is in P, else U(e)=W(e). Compute Q, an MST of G under U. If Cost(U,Q) < Cost(U,P) then Q≠P. But Cost(W,Q) = Cost(W,P) by construction of δ and ε. Hence P and Q are distinct MSTs of G under W. If Cost(U,Q) ≥ Cost(U,P) then Q=P and distinct MSTs of G under W do not exist.
The method above determines if there are at least two distinct MSTs, in time O(h(n,m)) if O(h(n,m)) bounds the time to find an MST of G.
I don't know if a similar method can treat whether three (or more) distinct MSTs exist; simple extensions of it fall to simple counterexamples.

Resources