Check if a given tree graph is linear or not
Last Updated :
07 Sep, 2022
Given a tree check if it is linear or not.
1
/ \
2 3
Linear as we can
form a line 2 1 3
1
/ \
2 3
/ \
4 5
Not linear
Examples:
Input : 3
1 2
1 3
Output :
YES
Explanation:
The Tree formed is 2-1-3 which is a linear one.
Input :
4
1 2
2 3
4 2
Output :
NO
Approach: The given tree would be linear only if n-2 of its nodes have indegree == 2 or number of nodes, n==1.
Implementation:
C++
#include<bits/stdc++.h>
using namespace std;
class Graph
{
public :
int V;
Graph( int v)
{
V = v;
}
void addEdge(vector< int > adj[], int v, int w)
{
adj[v].push_back(w);
adj[w].push_back(v);
}
bool isLinear(vector< int > adj[])
{
if (V == 1)
return true ;
int count = 0;
for ( int i = 0; i < V; i++)
{
if (adj[i].size() == 2)
count++;
}
if (count == V - 2)
return true ;
else
return false ;
}
};
int main()
{
Graph g1(3);
vector< int > adj[g1.V];
g1.addEdge(adj, 0, 1);
g1.addEdge(adj, 0, 2);
if (g1.isLinear(adj))
cout << "YES" ;
else
cout << "NO" ;
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class Graph {
private int V;
private LinkedList<Integer> adj[];
Graph( int v)
{
V = v;
adj = new LinkedList[v];
for ( int i = 0 ; i < v; ++i)
adj[i] = new LinkedList<Integer>();
}
void addEdge( int v, int w)
{
adj[v].add(w);
adj[w].add(v);
}
boolean isLinear()
{
if (V == 1 )
return true ;
int count = 0 ;
for ( int i = 0 ; i < V; i++) {
if (adj[i].size() == 2 )
count++;
}
if (count == V - 2 )
return true ;
else
return false ;
}
public static void main(String args[])
{
Graph g1 = new Graph( 3 );
g1.addEdge( 0 , 1 );
g1.addEdge( 0 , 2 );
if (g1.isLinear())
System.out.println( "YES" );
else
System.out.println( "NO" );
}
}
|
Python3
class Graph:
def __init__( self , v):
self .V = v
self .adj = [[] for i in range (v)]
def addEdge( self , v, w):
self .adj[v].append(w)
self .adj[w].append(v)
def isLinear( self ):
if ( self .V = = 1 ):
return True
count = 0
for i in range ( self .V):
if ( len ( self .adj[i]) = = 2 ):
count + = 1
if (count = = self .V - 2 ):
return True
else :
return False
if __name__ = = '__main__' :
g1 = Graph( 3 )
g1.addEdge( 0 , 1 )
g1.addEdge( 0 , 2 )
if (g1.isLinear()):
print ( "YES" )
else :
print ( "NO" )
|
C#
using System;
using System.Collections.Generic;
public class Graph
{
private int V;
private List< int > []adj;
Graph( int v)
{
V = v;
adj = new List< int >[v];
for ( int i = 0; i < v; ++i)
adj[i] = new List< int >();
}
void addEdge( int v, int w)
{
adj[v].Add(w);
adj[w].Add(v);
}
bool isLinear()
{
if (V == 1)
return true ;
int count = 0;
for ( int i = 0; i < V; i++)
{
if (adj[i].Count == 2)
count++;
}
if (count == V - 2)
return true ;
else
return false ;
}
public static void Main(String []args)
{
Graph g1 = new Graph(3);
g1.addEdge(0, 1);
g1.addEdge(0, 2);
if (g1.isLinear())
Console.WriteLine( "YES" );
else
Console.WriteLine( "NO" );
}
}
|
Javascript
<script>
let V;
let adj;
function addEdge(v, w)
{
adj[v].push(w);
adj[w].push(v);
}
function isLinear()
{
if (V == 1)
return true ;
let count = 0;
for (let i = 0; i < V; i++)
{
if (adj[i].length == 2)
count++;
}
if (count == V - 2)
return true ;
else
return false ;
}
V = 3;
adj = new Array(V)
for (let i = 0; i < V; ++i)
adj[i] = [];
addEdge(0, 1);
addEdge(0, 2);
if (isLinear())
document.write( "YES" );
else
document.write( "NO" );
</script>
|
Share your thoughts in the comments
Please Login to comment...