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.
Related
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. :-)
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.
Given a mixed acyclic graph consists of directed and undirected edges, I want to decompose this graph into a directed graph of chain components (each node within a chain component will be connected to each other only with undirected edges) and their orderings.
I am confused whether I should first topologically sort all directed edges, and then hunt undirected edges as chain components, or first should I go over all undirected edges and give them group id's and then find some directed edges to connect those components.
Since the graph is acyclic, I think it's possible to order them from low-numbered components to high-numbered ones, but couldn't come up with a solid answer.
I think both of your methods would work fine.
To my mind the second method seems more natural.
If I was doing this in networkx I would implement your second method by:
Create a new graph H containing all vertices but only the undirected edges.
Call connected_components on H to extract the chain components and assign each component a different group id.
Create a new graph F with 1 node for each group id. Connect groups in F with directed edges based on the directed edges in the original graph.
Call topological_sort on F to compute the ordering of the group ids.
The equivalence relation that defines a chain component is the following by Drton 2009:
Define two vertices v_0 and v_k in a chain graph G to be equivalent if there exists a path (v_0,..., v_k) such that v_i − v_{i+1} in G for all 0 ≤ i ≤ k − 1.
The equivalence classes under this equivalence relation are the chain components of G. Rougly speaking this means all connected components of the graph made from undirected edges of the chain graph, plus all the nodes that are incident with only directed edges, plus all the nodes that have no neighbors at all, which amounts to Peter's answer
Here is the function that correctly decomposes the chain graph CH-Asia given in Cowell 2005,Probabilistic Networks and ..., p. 110 Fig. 6.1.
It is part of a graphical models library I have been developing as hobby project.
Though it uses custom data structures, it should not be too hard to adapt to other code bases involving graphical models.
def get_chain_components(self) -> Set[Set[Node]]:
"""!
"""
# filter out undirected edges
edges = set()
for e in self.edges():
if e.type() == EdgeType.UNDIRECTED:
edges.add(e)
# make a graph from undirected edges
undi = UndiGraph.from_graph(Graph.from_edge_node_set(edges, self.nodes()))
return undi.get_components_as_node_set()
A mixed graph such you describe is again a directed graph. Simply replace each undirected edge with two directed ones pointing in opposite directions.
Also you can't have an acyclic graph that has undirected edges. At least a cycle of length 2 will always exist, so I am not sure what do you mean by this.
It seems you are looking for the strongly connected components in this graph so I advice you to use Tarjan's algorith for finding them.
I'm currently working on an implementation of the D*Lite algorithm from Sven Koenig.
http://idm-lab.org/bib/abstracts/papers/aaai02b.pdf. Basically I'm trying to understand all the details before starting to implement it. It seems that the algorithm works on directed graphs, that's the way to define the Pred and Succ functions.
How do I define the direction of the graphs and which the parameters decide the direction of the graphs. Should I use the value of some parameter like the g cost (which doesn't seem to be a good choice...since is the g cost along with the rhs value the one the algorithm updates) or the heuristic estimate of the distance?
D*, and D*-lite will both work on both directed and undirected graphs.
A graph is G = (V, E), where V is a list of configurations (or states) that can be reached. E is a list of the connections between vertices. In a directed graph, E is a set of edges which are ordered pairs (u, v), where both u and v are vertices. In an undirected graph, E is a set of unordered pairs.
Planning on an undirected graph is equivalent to planning on a directed graph, with a bidirectional edge. That is, if (u,v) is an edge (v, u) will also be an edge.
How you construct the graphs is application specific, and varies from simple grids to much more complex strategies like lattice approximations to forward kinematics.
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.