Reputation: 943
A singly connected graph is a directed graph which has at most 1 path from u to v ∀ u,v.
I have thought of the following solution:
O(V+E)
Is this right? Or is there a better solution.
Update : atmost 1 simple path.
Upvotes: 3
Views: 9361
Reputation: 1
A simple algorithm for solving this decision problem performs a depth-first search (DFS) starting from each vertex, terminating the DFS after it visits all vertices reachable from the start vertex.
If we encounter the same vertex along two different simple paths from the initial vertex, the graph is not singly-connected. Conversely, if we find no such case for any initial vertex, the graph is singly-connected.
def DFS(self, node: int, visited: List[int]) -> None:
"""
Performs DFS on the graph
"""
visited[node] = 1
for neighbour in self.nodes[node]:
if self.SC_ORIENTED == 0:
return
if visited[neighbour] == 2:
self.SC_ORIENTED = 0
return
if visited[neighbour] == 0:
self.DFS(neighbour, visited)
visited[node] = 2
def is_SC_oriented(self) -> bool:
"""
self.nodes: dict with nodes as keys and set of its neighbours as values
"""
if len(self.get_undirected_edges()) != 0:
raise Exception("Graph is not undirected!")
self.SC_ORIENTED = -1
visited = [0]*len(self.nodes)
for node in self.nodes:
if visited[node] == 0:
self.DFS(node, visited)
if self.SC_ORIENTED == 0:
return False
return True
This algorithm runs in O(V*E) time. A more efficient algorithm which runs in O(V^2) time can be seen found in the following paper.
Adam L. Buchsbaum and Martin C. Carlisle. Determining uni-connectivity in directed graphs. Information Processing Letters, 48(1):9–12, 1993.
Upvotes: 0
Reputation: 579
Is this right?
No, it's not right. Considering the following graph which is not singly connected. The first component comes from a dfs beginning with vertex b and the second component comes from a dfs beginning with vertex a.
Do the DFS, the graph is singly connected if all of the three following conditions satisfies:
Upvotes: -1
Reputation: 656
A graph is not singly connected if one of the two following conditions satisfies:
In the same component, when you do the DFS, you get a road from a vertex to another vertex that has already finished it's search (when it is marked BLACK)
When a node points to >=2 vertices from another component, if the 2 vertices have a connection then it is not singly connected. But this would require you to keep a depth-first forest.
Upvotes: 2
Reputation: 11
A singly connected component is any directed graph belonging to the same entity. It may not necessarily be a DAG and can contain a mixture of cycles.
Every node has atleast some link(in-coming or out-going) with atleast one node for every node in the same component. All we need to do is to check whether such a link exists for the same component.
Singly Connected Component could be computed as follows:
Run an iteration over all nodes. If all the nodes have the same common leader, the undirected version of the graph is singly connected.
Else, it contains of multiple singly connected subgraphs represented by their corresponding leaders.
Upvotes: 0