Connected undirected acyclic graphs versus trees - data-structures

While I'm studying about graph theory with MIT book 'introduction to algorithm', I was referred to some definitions about graphs and trees.
In MIT's Introduction to Algorithm 3rd Edition book, the appendix tree chapter shows me Theorem B.2, "Properties of Free Trees"
Let G = (V,E) be an undirected graph. The following statements are equivalent.
G is a free tree
...
G is acyclic, and |E| = |V| - 1.
Is there an example of connected, undirected, acyclic graph which is not a tree?
Theoritically, if there is a undirected acyclic graph which satisfies the condition that |E| =! |V| - 1, would that work as an example?
If there is an example satisfies that condition, can you show me?

Any connected acyclic graph is a tree. There are several different equivalent definitions of trees:
They're connected acyclic graphs.
They're connected graphs with one more node than edge.
They're minimally-connected graphs (they're connected, but removing any edge disconnects them)
They're maximally acyclic graphs (they're acyclic, and adding any missing edge creates a cycle)
They're graphs where any two nodes have exactly one simple path between them.
So no, you can't find a connected acyclic graph that isn't a tree. :-)

Related

Can there be self loops for undirected graphs?

I mean directed graphs can have a self-loop, so I don't see the reason why an undirected graph cannot have it (CLRS says it's forbidden without giving a valid reason).
Example:
G_directed = (V,E) is a directed graph
Say this graph has the vertex set V = {1,2,3,4,5,6}
With edges E = {(1,2),(2,2),(2,4),(2,5),(4,1),(4,5),(5,4),(6,3)}
-----------------------------------------------------------------
Say we now decide to turn G_directed into an undirected graph:
G_undirected = (Vu,Eu) is an undirected graph
Vu = {1,2,3,4,5,6}
With edges E = {(1,2),(2,2),(2,4),(2,5),(4,1),(4,5),(6,3)}
In the example (2,2) is the self loop. I seriously don't see any problems this can have with graph traversals.
There are several categories of undirected graphs. Where loops (self references) are not allowed, they are called simple graphs. But there is indeed no reason to consider undirected graphs with loops and even multiple edges between the same pair of nodes: these are called multigraphs:
A loop is an edge (directed or undirected) that connects a vertex to itself; it may be permitted or not, according to the application.
A multigraph, as opposed to a simple graph, is an undirected graph in which multiple edges (and sometimes loops) are allowed.

Spanning Tree VS. Spanning Forest

What's the difference between a Spanning Tree and a Spanning Forest in graphs, conceptually.
Also, is it possible to construct a Spanning Forest through DFS or BFS traversals? Why? How?
I understand the Spanning Tree, but I couldn't find any clear explanations about spanning forests. Even Wikipedia (https://en.wikipedia.org/wiki/Spanning_tree), doesn't give a clear definition about it.
My book (Data Structures & Algorithms, Wiley - sixth edition) also has no definition for spanning forests.
I was wondering, if we have a graph with for example three connected components in it, is it possible to construct a spanning forest by DFS/BFS traversals?
When there is only one connected component in your graph, the spanning tree = spanning forest.
But when there are multiple connected components in your graph. For example in following picture we have 3 connected components.:
So for each component, we will have a spanning tree, and all 3 spanning trees will constitute spanning forest
I was wondering, if we have a graph with for example three connected
components in it, is it possible to construct a spanning forest by
DFS/BFS traversals?
Yes it is possible. When there is only 1 connected component, your BFS or DFS will terminate visiting all the vertices and you will have a spanning tree (which in this case is equal to spanning forest).
But when you have more than 1 connected component, like in the picture, the only thing you have to do is start another BFS or DFS from an unvisited vertex. Your algorithm terminates when there is no unvisited vertex left and each BFS or DFS traversal will yield a spanning tree.
Non-trivial spanning forests can be constructed even for complete graphs via the following algorithm:
preconditions
all vertices are unmarked
the edges are enumerated from 1 to m
edge processing
if both of its adjacent vertices are marked, skip it because then that edge would either merge trees of the forest or creates a cycle in one of its trees
else mark its unmarked adjacent vertices
algorithm
process the edges in the order of their enumeration
explanaton:
while it is feasible in the construction of spanning trees to add edges that "bridge" connected components, those edges are not added in the above algorithm.
interpretation:
if the edges are enumerated according to ascending length, the edges of the resulting spanning forest will be a subsets of the MST and the trees of the forest will resemble "basins" i.e. the length of edges is smallest for the one that created the connected component and increases with every edge that is attached in later steps.
In that case the properties of spanning forest may provide insight into the structural properties of the original graph and/or be utilized in algorithms.

Determining whether or not a directed or undirected graph is a tree

I would like to know of a fast algorithm to determine if a directed or undirected graph is a tree.
This post seems to deal with it, but it is not very clear; according to this link, if the graph is acyclic, then it is a tree. But if you consider the directed and undirected graphs below: in my opinion, only graphs 1 and 4 are trees. I suppose 3 is neither cyclic, nor a tree.
What needs to be checked to see if a directed or undirected graph is a tree or not, in an efficient way? And taking it one step ahead: if a tree exists then is it a binary tree or not?
For a directed graph:
Find the vertex with no incoming edges (if there is more than one or no such vertex, fail).
Do a breadth-first or depth-first search from that vertex. If you encounter an already visited vertex, it's not a tree.
If you're done and there are unexplored vertices, it's not a tree - the graph is not connected.
Otherwise, it's a tree.
To check for a binary tree, additionally check if each vertex has at most 2 outgoing edges.
For an undirected graph:
Check for a cycle with a simple depth-first search (starting from any vertex) - "If an unexplored edge leads to a node visited before, then the graph contains a cycle." If there's a cycle, it's not a tree.
If the above process leaves some vertices unexplored, it's not a tree, because it's not connected.
Otherwise, it's a tree.
To check for a binary tree, if the graph has more than one vertex, additionally check that all vertices have 1-3 edges (1 to the parent and 2 to the children).
Checking for the root, i.e. whether one vertex contains 1-2 edges, is not necessary as there has to be vertices with 1-2 edges in an acyclic connected undirected graph.
Note that identifying the root is not generically possible (it may be possible in special cases) as, in many undirected graphs, more than one of the nodes can be made the root if we were to make it a binary tree.
If an undirected given graph is a tree:
the graph is connected
the number of edges equals the number of nodes - 1.
An undirected graph is a tree when the following two conditions are true:
The graph is a connected graph.
The graph does not have a cycle.
A directed graph is a tree when the following three conditions are true:
The graph is a connected graph.
The graph does not have a cycle.
Each node except root should have exactly one parent.

What is a Acyclic connected undirected graph?

I was going through lecture of Minimum Spanning Tree, It says we are supposed to find connected acyclic subgraph graph in a Undirected graphs.
My question is that How can a connected undirected graph be a Acyclic, Since it is connected you can move to any vertex from any vertex.
Can anyone tell me what I am doing wrong?
It's really just a matter of definition. See http://en.wikipedia.org/wiki/Cycle_(graph_theory). What you seem to refer to as a cycle, is what is called a closed walk in the article: any path from a vertex to itself. As you have said yourself, using that definition, any connected undirected graph contains cycles. However, if you require that the subpath from the second to the last vertex be a simple path (hence simple cycle), i.e. one containing no repeating vertices, you end up with many connected undirected graphs which are in fact acyclic, such as trees for instance. Obviously, the path also has to contain at least 3 edges, else any (A,B,A) would be a cycle.
Consider the following graphs
A A
1) / \ 2) / \
B C B - C
only 2) contains simple cycles, so 1) is acyclic.

How to remove cycles in an unweighted directed graph, such that the number of edges is maximised?

Let G be an unweighted directed graph containing cycles. I'm looking for an algorithm which finds/creates all acyclic graphs G', composed of all vertices in G and a subset of edges of G, just small enough to make G' acyclic.
More formal: The desired algorithm consumes G and creates a set of acyclic graphs S, where each graph G' in S satisfies following properties:
G' contains all vertices of G.
G' contains a subset of edges of G, such that G' is acyclic.
The number of edges of G' is maximised. Which means: There is no G'' satisfying properties 1 and 2, such that G'' contains more edges then G' and G'' is acyclic.
Background: The original graph G models a pairwise ordering between elements. This can't be exploited as an ordering over all elements due to cycles in the graph. The maximal acyclic graphs G' therefore should model a best-possible approximation to this ordering, trying to respect as much of the pairwise ordering relation as possible.
In a naive approach, one could remove all possible combinations of edges and check for acyclicity after each removal. In this case there is a strongly branching tree of variations meaning bad time and space complexity.
Note: The problem may be related to a spanning tree, and you could define the G' graphs as a kind of directed spanning tree. But keep in mind that in my scenario a pair of edges in G' may have the same starting or the same ending vertex. This conflicts with some definitions of directed spanning trees used in literature.
EDIT: Added intuitive description, background information and note related to spanning trees.
This problem is called Feedback Arc Set. Since it is NP-hard, it is unlikely that you will find a scalable fast algorithm. However, if your instances are small, then algorithms such as the one from the paper “On enumerating all minimal solutions of feedback problems” by B. Schwikowski and E. Speckenmeyer might work.

Resources