I came to a problem when I was trying to work with graphs and write some code for it but with no luck :/ !!
I wanted to created something that will take the data of the graph and check if it is: 1- connected 2- bipartite 3- has cycle 4- is a tree
so I was wondering for example if this can be written to read a graph data from a .txt file that will do the above tests ??
using simple edge-list format is the correct approach for it ?
your help is appreciated if you can give me a link to read about how to do this task or a kick start for a code !!
thanks :D
Algorithm to check if a graph is Bipartite: One approach is to check whether the graph is 2-colorable or not using backtracking algorithm m coloring problem.
Of course, bipartite graphs can have even cycles, which starts in one independent set and ends there. We can represent the independent sets using colors. Theorem (König, 1936) A graph is bipartite if and only if it has no odd cycle.
Every tree is bipartite. Cycle graphs with an even number of vertices are bipartite. Every planar graph whose faces all have even length is bipartite.
There is a unique path between any 2 vertices in a tree. Every tree with at least 2 vertices has at least 2 vertices of degree 1. Every tree is bipartite. Removing any edge from a tree will separate the tree into 2 connected components.
check if it is:
- connected
For this one, you try to traverse the entire graph from one point, and see if you succeed. Both depth-first traversal and breadth-first traversal are acceptable here. This algorithm will split the node into components:
c
, if this node is unvisited
t
enqueued
If there is only one component, the graph is connected.
If a queue is used, the algorithm is a breadth-first search. If a stack is used, the algorithm is a depth-first search. Any other collection with the push and pop-any operations will do. Of special interest is the call-stack: replace "enqueue for traversal" with "traverse recursively"
For directed graphs, there are two related concepts: A directed graph is weakly connected iff it is connected neglecting all edge directions. A directed graph is strongly connected iff every node is reachable from every node. To test for strong connectedness it sufficces to check every node is reachable from the first node using only forward edges, and every node is reachable from the first node using only backwards edges (the first node is reachable from every node).
- bipartite
A graph is bipartite iff it is bicolorable. Try to assign a bicoloring, and if you fail, the graph is not bipartite. This can be incorporated into the previous algorithm: Whenever you mark a node as open, assign a color to it, opposite to the color assigned to its neighbor t
. When a neighbor of t
is already open, check its color. If its color is the same as that of t
, there is no bicoloring.
- has cycle
This one is easy: If you observe any node that is already open, there is a cycle. Note that every graph that has no cycle is bipartite.
In directed graphs, this will detect the presence of an undirected cycle. To detect the presence of directed cycles, use the following (topological sort) algorithm:
- tree
An undirected graph is a tree iff it is connected and contains no cycle.
A directed graph is a rooted tree iff it has no undirected cycles and there is only one vertex with an indegree of zero (only one root). Also, a directed graph is a rooted tree iff it is connected, has no undirected cycles and every node with an outdegree of zero has an indegree of at most one (every sink is a leaf).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With