What is the difference between a directed and undirected graph - data-structures

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.

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.

Relationship between vertices and edges in directed graph

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.

Transform a simple directed graph to a simple undirected graph

How would I convert a simple directed graph to a simple undirected one?
Is this possible?
Assuming: (from here)
in a simple digraph loops are disallowed. (A loop is an arc that pairs a vertex to itself.)
and: (from here)
a simple [undirected] graph is an undirected graph that has no loops (edges connected at both ends to the same vertex) and no more than one edge between any two different vertices.
I'm assuming the edges are unweighted, otherwise this can't be done without specifying how this needs to be done.
AM = Adjacency matrix
AL = Adjacency list
Remove the direction of all edges.
AM: For each edge A->B, row A, column B would already be set. Also set row B, column A.
AL: For each edge A->B, the edge already appears in A's AL, also add the edge to B's AL.
Go through all edges, removing all edges for which we already found an edge between the two vertices that are the end-points of that edge (so if A-B has already been processed, if we find another A-B (or equivalently B-A), we must remove that edge). This needs to be done since there can be multiple edges between vertices in a simple directed graph, but this can't happen in a simple undirected graph.
AM: Nothing really needs to be done since an AM forces there to only be a single edge between vertices, so we'd just be overwriting the edges.
AL: For each vertex A, for each edge A-B or B-A in its adjacency list, remove all other edges A-B or B-A in the list.
Source and details...
For each directed edge e=(x,y), add new vertices ve1,…,ve5 and replace e with the edges xve1, ve1ve2, ve1ve3, ve3ve4, ve4ve5, ve3y.
To decode, every leaf (degree-1 vertex) whose neighbour has degree 2 must be ve5 for some edge e=(x,y); its neighbour is ve4 and the other neighbour of ve4 is ve3. ve3 has a unique neighbour that has both degree 3 and is adjacent to a leaf: the neighbour is ve1 and its leaf is ve2 (if ve1 has two leaf neighbours, pick one arbitrarily to be ve2). The other neighbour of ve1 is x and the other neighbour of ve3 is y. Restore the directed edge (x,y) and delete the vertices ve1,…,ve5.
G=(V,E) is a directed graph. (a,b) \in E is a directed edge.
G' = (V, E') is an equivalent undirected graph where (a,b) is transformed as (a,b) and (b,a).
Thus, edge directed edge becomes two edge (one in each direction).

Basic Questions about Minimum Spanning Tree

This is not a homework. I am trying to do exercises from a textbook to understand MST (minimum spanning tree).
Suppose I have a cycle C in a weighted undirected graph G. As I understand, the following is correct:
The heaviest edge in C belongs to no MST of G. That is, there is no MST of G, which contains that edge.
The lightest edge in C belongs to some MST of G. That is, there is an MST of G, which contains that edge.
Now I wonder if the followings claims are correct too.
The lightest edge in C belongs to all MST of G. That is, there is no MST of G, which does not contain that edge.
Any edge in C except the heaviest one belongs to some MST. That is, for each edge in C except the heaviest one there is an MST, which contains that edge.
Could you prove the last claim?
Even for the first claim if there are multiple edges which are lightest, all need not be included in the MST.
The first one of your claims is always true. The lightest edge is on the MST for any graph.
The second one is not always true. It is always true if the entire graph is a
cycle and thus every node has 2 edges incident to it. However, in the general case,
an edge (u,v) of weight k is never on MST whenever there is a path between the nodes u and v
connecting them at a total weight less than k.
I don't think your claims are valid. The problem is that you are only considering a cycle in a larger graph.
Consider for example a graph G consisting of 6 nodes in a cycle (with random weights >1). Your claims might hold for that graph but now add 1 node in the center of the graph and connect it with 6 links of cost 1. The MST of your entire graph now will consist of only those 6 edges (which form a star).
If you now look at your claims, you'll see:
The lightest edge in your cycle does not belong to the MST (=star)
None of the edges in the cycle are in the MST

Resources