Print the combinations of given vertices which connect of M edges they not contain cycle and its path cost is positive by node

Here given code implementation process.

//  C program for
// Print the combinations of given vertices which connect of M edges 
// they not contain cycle and its path cost is positive by node
#include <stdio.h>

void findCombination(int vertex[], 
  int result[], int visit[], 
    int cost, 
      int count, 
      	int n, 
          int edge)
{
	if (count - 1 == edge && cost > 0)
	{
		for (int i = 0; i <= edge; ++i)
		{
			if (i != 0)
			{
				printf(" ⤑ ");
			}
			printf("%d", result[i]);
		}
		printf(" : %d\n", cost);
	}
	if (count >= n || count - 1 >= edge || 
        (count >= 2 && cost == 0))
	{
		// Base case
		return;
	}
	for (int i = 0; i < n; ++i)
	{
		if (visit[i] == -1)
		{
			visit[i] = i;
			// Collect resultant node
			result[count] = vertex[i];
			if (count == 0)
			{
				// Find combinations using recursively
				findCombination(vertex, result, 
                                visit, vertex[i], count + 1, n, edge);
			}
			else
			{
				// Find combinations using recursively
				findCombination(vertex, 
                                result, visit, vertex[i] & cost, 
                                count + 1, n, edge);
			}
			visit[i] = -1;
		}
	}
}
void combination(int vertex[], int n, int edge)
{
	if (n <= 0 || edge >= n || edge <= 0)
	{
		return;
	}
	int result[edge + 1];
	int visit[n];
	for (int i = 0; i < n; ++i)
	{
		visit[i] = -1;
	}
	// Find combination pair
	findCombination(vertex, result, visit, 0, 0, n, edge);
}
int main(int argc, char
	const *argv[])
{
	// Graph vertex
	// Its must be positive because it indicates index node id.
	// Assume that given vertex are distinct because we not need cycle.
	int vertex[] = {
		2 , 16 , 5 , 6 , 7 , 1
	};
	// Get the number of nodes
	int n = sizeof(vertex) / sizeof(vertex[0]);
	// number of edge
	int m = 2;
	/*
	    arr [] = [2, 16, 5, 6, 7, 1]
	    -----------------------------
	    2 ⤑ 6 ⤑ 7  (2&6&7) : 2
	    2 ⤑ 7 ⤑ 6  (2&7&6) : 2
	    5 ⤑ 6 ⤑ 7  (5&6&7) : 4
	    5 ⤑ 7 ⤑ 6  (5&7&6) : 4
	    5 ⤑ 7 ⤑ 1  (5&7&1) : 1
	    5 ⤑ 1 ⤑ 7  (5&1&7) : 1
	    6 ⤑ 2 ⤑ 7  (6&2&7) : 2
	    6 ⤑ 5 ⤑ 7  (6&5&7) : 4
	    6 ⤑ 7 ⤑ 2  (6&7&2) : 2
	    6 ⤑ 7 ⤑ 5  (6&7&5) : 4
	    7 ⤑ 2 ⤑ 6  (7&2&6) : 2
	    7 ⤑ 5 ⤑ 6  (7&5&6) : 4
	    7 ⤑ 5 ⤑ 1  (7&5&1) : 1
	    7 ⤑ 6 ⤑ 2  (7&6&2) : 2
	    7 ⤑ 6 ⤑ 5  (7&6&5) : 4
	    7 ⤑ 1 ⤑ 5  (7&1&5) : 1
	    1 ⤑ 5 ⤑ 7  (1&5&7) : 1
	    1 ⤑ 7 ⤑ 5  (1&7&5) : 1
	  -------------------------
	    Note m = 2 so m+1 node is possible in result
	*/
	combination(vertex, n, 2);
	return 0;
}

Output

2 ⤑ 6 ⤑ 7 : 2
2 ⤑ 7 ⤑ 6 : 2
5 ⤑ 6 ⤑ 7 : 4
5 ⤑ 7 ⤑ 6 : 4
5 ⤑ 7 ⤑ 1 : 1
5 ⤑ 1 ⤑ 7 : 1
6 ⤑ 2 ⤑ 7 : 2
6 ⤑ 5 ⤑ 7 : 4
6 ⤑ 7 ⤑ 2 : 2
6 ⤑ 7 ⤑ 5 : 4
7 ⤑ 2 ⤑ 6 : 2
7 ⤑ 5 ⤑ 6 : 4
7 ⤑ 5 ⤑ 1 : 1
7 ⤑ 6 ⤑ 2 : 2
7 ⤑ 6 ⤑ 5 : 4
7 ⤑ 1 ⤑ 5 : 1
1 ⤑ 5 ⤑ 7 : 1
1 ⤑ 7 ⤑ 5 : 1
// Java Program 
// Print the combinations of given vertices which connect of M edges 
// they not contain cycle and its path cost is positive by node
public class Combinations
{
	public void findCombination(int[] vertex, 
  	int[] result, int[] visit, int cost, 
  	int count, int n, int edge)
	{
		if (count - 1 == edge && cost > 0)
		{
			for (int i = 0; i <= edge; ++i)
			{
				if (i != 0)
				{
					System.out.print(" ⤑ ");
				}
				System.out.print(result[i]);
			}
			System.out.print(" : " + cost + "\n");
		}
		if (count >= n || count - 1 >= edge || 
            (count >= 2 && cost == 0))
		{
			// Base case
			return;
		}
		for (int i = 0; i < n; ++i)
		{
			if (visit[i] == -1)
			{
				visit[i] = i;
				// Collect resultant node
				result[count] = vertex[i];
				if (count == 0)
				{
					// Find combinations using recursively
					findCombination(vertex, result, 
                                    visit, vertex[i], 
                                    count + 1, n, edge);
				}
				else
				{
					// Find combinations using recursively
					findCombination(vertex, 
                                    result, visit, vertex[i] & cost, 
                                    count + 1, n, edge);
				}
				visit[i] = -1;
			}
		}
	}
	public void combination(int[] vertex, int n, int edge)
	{
		if (n <= 0 || edge >= n || edge <= 0)
		{
			return;
		}
		int[] result = new int[edge + 1];
		int[] visit = new int[n];
		for (int i = 0; i < n; ++i)
		{
			visit[i] = -1;
		}
		// Find combination pair
		findCombination(vertex, result, visit, 0, 0, n, edge);
	}
	public static void main(String args[])
	{
		Combinations task = new Combinations();
		// Graph vertex
		// Its must be positive because it indicates index node id.
		// Assume that given vertex are distinct.
		int[] vertex = {
			2 , 16 , 5 , 6 , 7 , 1
		};
		// Get the number of nodes
		int n = vertex.length;
		// number of edge
		int m = 2;
		/*
		    arr [] = [2, 16, 5, 6, 7, 1]
		    -----------------------------
		    2 ⤑ 6 ⤑ 7  (2&6&7) : 2
		    2 ⤑ 7 ⤑ 6  (2&7&6) : 2
		    5 ⤑ 6 ⤑ 7  (5&6&7) : 4
		    5 ⤑ 7 ⤑ 6  (5&7&6) : 4
		    5 ⤑ 7 ⤑ 1  (5&7&1) : 1
		    5 ⤑ 1 ⤑ 7  (5&1&7) : 1
		    6 ⤑ 2 ⤑ 7  (6&2&7) : 2
		    6 ⤑ 5 ⤑ 7  (6&5&7) : 4
		    6 ⤑ 7 ⤑ 2  (6&7&2) : 2
		    6 ⤑ 7 ⤑ 5  (6&7&5) : 4
		    7 ⤑ 2 ⤑ 6  (7&2&6) : 2
		    7 ⤑ 5 ⤑ 6  (7&5&6) : 4
		    7 ⤑ 5 ⤑ 1  (7&5&1) : 1
		    7 ⤑ 6 ⤑ 2  (7&6&2) : 2
		    7 ⤑ 6 ⤑ 5  (7&6&5) : 4
		    7 ⤑ 1 ⤑ 5  (7&1&5) : 1
		    1 ⤑ 5 ⤑ 7  (1&5&7) : 1
		    1 ⤑ 7 ⤑ 5  (1&7&5) : 1
		  -------------------------
		    Note m = 2 so m+1 node is possible in result
		*/
		task.combination(vertex, n, 2);
	}
}

Output

2 ⤑ 6 ⤑ 7 : 2
2 ⤑ 7 ⤑ 6 : 2
5 ⤑ 6 ⤑ 7 : 4
5 ⤑ 7 ⤑ 6 : 4
5 ⤑ 7 ⤑ 1 : 1
5 ⤑ 1 ⤑ 7 : 1
6 ⤑ 2 ⤑ 7 : 2
6 ⤑ 5 ⤑ 7 : 4
6 ⤑ 7 ⤑ 2 : 2
6 ⤑ 7 ⤑ 5 : 4
7 ⤑ 2 ⤑ 6 : 2
7 ⤑ 5 ⤑ 6 : 4
7 ⤑ 5 ⤑ 1 : 1
7 ⤑ 6 ⤑ 2 : 2
7 ⤑ 6 ⤑ 5 : 4
7 ⤑ 1 ⤑ 5 : 1
1 ⤑ 5 ⤑ 7 : 1
1 ⤑ 7 ⤑ 5 : 1
// Include header file
#include <iostream>
using namespace std;
// C++ Program
// Print the combinations of given vertices which connect of M edges
// they not contain cycle and its path cost is positive by node
class Combinations
{
	public: void findCombination(int vertex[], 
      int result[], int visit[], int cost, 
        int count, int n, int edge)
	{
		if (count - 1 == edge && cost > 0)
		{
			for (int i = 0; i <= edge; ++i)
			{
				if (i != 0)
				{
					cout << " ⤑ ";
				}
				cout << result[i];
			}
			cout << " : " << cost << "\n";
		}
		if (count >= n || count - 1 >= edge || 
            (count >= 2 && cost == 0))
		{
			// Base case
			return;
		}
		for (int i = 0; i < n; ++i)
		{
			if (visit[i] == -1)
			{
				visit[i] = i;
				// Collect resultant node
				result[count] = vertex[i];
				if (count == 0)
				{
					// Find combinations using recursively
					this->findCombination(vertex, 
                                          result, visit, 
                                          vertex[i], count + 1, 
                                          n, edge);
				}
				else
				{
					// Find combinations using recursively
					this->findCombination(vertex, 
                                          result, visit, 
                                          vertex[i] &cost, 
                                          count + 1, n, edge);
				}
				visit[i] = -1;
			}
		}
	}
	void combination(int vertex[], int n, int edge)
	{
		if (n <= 0 || edge >= n || edge <= 0)
		{
			return;
		}
		int result[edge + 1];
		int visit[n];
		for (int i = 0; i < n; ++i)
		{
			visit[i] = -1;
		}
		// Find combination pair
		this->findCombination(vertex, result, visit, 0, 0, n, edge);
	}
};
int main()
{
	Combinations *task = new Combinations();
	// Graph vertex
	// Its must be positive because it indicates index node id.
	// Assume that given vertex are distinct.
	int vertex[] = {
		2 , 16 , 5 , 6 , 7 , 1
	};
	// Get the number of nodes
	int n = sizeof(vertex) / sizeof(vertex[0]);
	// number of edge
	int m = 2;
	/*
	    arr [] = [2, 16, 5, 6, 7, 1]
	    -----------------------------
	    2 ⤑ 6 ⤑ 7  (2&6&7) : 2
	    2 ⤑ 7 ⤑ 6  (2&7&6) : 2
	    5 ⤑ 6 ⤑ 7  (5&6&7) : 4
	    5 ⤑ 7 ⤑ 6  (5&7&6) : 4
	    5 ⤑ 7 ⤑ 1  (5&7&1) : 1
	    5 ⤑ 1 ⤑ 7  (5&1&7) : 1
	    6 ⤑ 2 ⤑ 7  (6&2&7) : 2
	    6 ⤑ 5 ⤑ 7  (6&5&7) : 4
	    6 ⤑ 7 ⤑ 2  (6&7&2) : 2
	    6 ⤑ 7 ⤑ 5  (6&7&5) : 4
	    7 ⤑ 2 ⤑ 6  (7&2&6) : 2
	    7 ⤑ 5 ⤑ 6  (7&5&6) : 4
	    7 ⤑ 5 ⤑ 1  (7&5&1) : 1
	    7 ⤑ 6 ⤑ 2  (7&6&2) : 2
	    7 ⤑ 6 ⤑ 5  (7&6&5) : 4
	    7 ⤑ 1 ⤑ 5  (7&1&5) : 1
	    1 ⤑ 5 ⤑ 7  (1&5&7) : 1
	    1 ⤑ 7 ⤑ 5  (1&7&5) : 1
	  -------------------------
	    Note m = 2 so m+1 node is possible in result
	*/
	task->combination(vertex, n, 2);
	return 0;
}

Output

2 ⤑ 6 ⤑ 7 : 2
2 ⤑ 7 ⤑ 6 : 2
5 ⤑ 6 ⤑ 7 : 4
5 ⤑ 7 ⤑ 6 : 4
5 ⤑ 7 ⤑ 1 : 1
5 ⤑ 1 ⤑ 7 : 1
6 ⤑ 2 ⤑ 7 : 2
6 ⤑ 5 ⤑ 7 : 4
6 ⤑ 7 ⤑ 2 : 2
6 ⤑ 7 ⤑ 5 : 4
7 ⤑ 2 ⤑ 6 : 2
7 ⤑ 5 ⤑ 6 : 4
7 ⤑ 5 ⤑ 1 : 1
7 ⤑ 6 ⤑ 2 : 2
7 ⤑ 6 ⤑ 5 : 4
7 ⤑ 1 ⤑ 5 : 1
1 ⤑ 5 ⤑ 7 : 1
1 ⤑ 7 ⤑ 5 : 1
// Include namespace system
using System;
// Csharp Program
// Print the combinations of given vertices which connect of M edges
// they not contain cycle and its path cost is positive by node
public class Combinations
{
	public void findCombination(int[] vertex, int[] result, 
  int[] visit, int cost, int count, int n, int edge)
	{
		if (count - 1 == edge && cost > 0)
		{
			for (int i = 0; i <= edge; ++i)
			{
				if (i != 0)
				{
					Console.Write(" ⤑ ");
				}
				Console.Write(result[i]);
			}
			Console.Write(" : " + cost + "\n");
		}
		if (count >= n || count - 1 >= edge || 
            (count >= 2 && cost == 0))
		{
			// Base case
			return;
		}
		for (int i = 0; i < n; ++i)
		{
			if (visit[i] == -1)
			{
				visit[i] = i;
				// Collect resultant node
				result[count] = vertex[i];
				if (count == 0)
				{
					// Find combinations using recursively
					this.findCombination(vertex, 
                                         result, visit, 
                                         vertex[i], count + 1, 
                                         n, edge);
				}
				else
				{
					// Find combinations using recursively
					this.findCombination(vertex, result, 
                                         visit, vertex[i] & cost, 
                                         count + 1, n, edge);
				}
				visit[i] = -1;
			}
		}
	}
	public void combination(int[] vertex, int n, int edge)
	{
		if (n <= 0 || edge >= n || edge <= 0)
		{
			return;
		}
		int[] result = new int[edge + 1];
		int[] visit = new int[n];
		for (int i = 0; i < n; ++i)
		{
			visit[i] = -1;
		}
		// Find combination pair
		this.findCombination(vertex, result, visit, 0, 0, n, edge);
	}
	public static void Main(String[] args)
	{
		Combinations task = new Combinations();
		// Graph vertex
		// Its must be positive because it indicates index node id.
		// Assume that given vertex are distinct.
		int[] vertex = {
			2 , 16 , 5 , 6 , 7 , 1
		};
		// Get the number of nodes
		int n = vertex.Length;
		// number of edge
		int m = 2;
		/*
		    arr [] = [2, 16, 5, 6, 7, 1]
		    -----------------------------
		    2 ⤑ 6 ⤑ 7  (2&6&7) : 2
		    2 ⤑ 7 ⤑ 6  (2&7&6) : 2
		    5 ⤑ 6 ⤑ 7  (5&6&7) : 4
		    5 ⤑ 7 ⤑ 6  (5&7&6) : 4
		    5 ⤑ 7 ⤑ 1  (5&7&1) : 1
		    5 ⤑ 1 ⤑ 7  (5&1&7) : 1
		    6 ⤑ 2 ⤑ 7  (6&2&7) : 2
		    6 ⤑ 5 ⤑ 7  (6&5&7) : 4
		    6 ⤑ 7 ⤑ 2  (6&7&2) : 2
		    6 ⤑ 7 ⤑ 5  (6&7&5) : 4
		    7 ⤑ 2 ⤑ 6  (7&2&6) : 2
		    7 ⤑ 5 ⤑ 6  (7&5&6) : 4
		    7 ⤑ 5 ⤑ 1  (7&5&1) : 1
		    7 ⤑ 6 ⤑ 2  (7&6&2) : 2
		    7 ⤑ 6 ⤑ 5  (7&6&5) : 4
		    7 ⤑ 1 ⤑ 5  (7&1&5) : 1
		    1 ⤑ 5 ⤑ 7  (1&5&7) : 1
		    1 ⤑ 7 ⤑ 5  (1&7&5) : 1
		  -------------------------
		    Note m = 2 so m+1 node is possible in result
		*/
		task.combination(vertex, n, m);
	}
}

Output

2 ⤑ 6 ⤑ 7 : 2
2 ⤑ 7 ⤑ 6 : 2
5 ⤑ 6 ⤑ 7 : 4
5 ⤑ 7 ⤑ 6 : 4
5 ⤑ 7 ⤑ 1 : 1
5 ⤑ 1 ⤑ 7 : 1
6 ⤑ 2 ⤑ 7 : 2
6 ⤑ 5 ⤑ 7 : 4
6 ⤑ 7 ⤑ 2 : 2
6 ⤑ 7 ⤑ 5 : 4
7 ⤑ 2 ⤑ 6 : 2
7 ⤑ 5 ⤑ 6 : 4
7 ⤑ 5 ⤑ 1 : 1
7 ⤑ 6 ⤑ 2 : 2
7 ⤑ 6 ⤑ 5 : 4
7 ⤑ 1 ⤑ 5 : 1
1 ⤑ 5 ⤑ 7 : 1
1 ⤑ 7 ⤑ 5 : 1
package main
import "fmt"
// Go Program
// Print the combinations of given vertices which connect of M edges
// they not contain cycle and its path cost is positive by node
type Combinations struct {}
func getCombinations() * Combinations {
	var me *Combinations = &Combinations {}
	return me
}
func(this Combinations) findCombination(vertex[] int, 
	result[] int, visit[] int, cost int, count int, n int, edge int) {
	if count - 1 == edge && cost > 0 {
		for i := 0 ; i <= edge ; i++ {
			if i != 0 {
				fmt.Print(" ⤑ ")
			}
			fmt.Print(result[i])
		}
		fmt.Print(" : ", cost, "\n")
	}
	if count >= n || count - 1 >= edge || (count >= 2 && cost == 0) {
		// Base case
		return
	}
	for i := 0 ; i < n ; i++ {
		if visit[i] == -1 {
			visit[i] = i
			// Collect resultant node
			result[count] = vertex[i]
			if count == 0 {
				// Find combinations using recursively
				this.findCombination(vertex, result, 
					visit, vertex[i], count + 1, n, edge)
			} else {
				// Find combinations using recursively
				this.findCombination(vertex, result, 
					visit, vertex[i] & cost, count + 1, n, edge)
			}
			visit[i] = -1
		}
	}
}
func(this Combinations) combination(vertex[] int, n int, edge int) {
	if n <= 0 || edge >= n || edge <= 0 {
		return
	}
	var result = make([] int, edge + 1)
	var visit = make([] int, n)
	for i := 0 ; i < n ; i++ {
		visit[i] = -1
	}
	// Find combination pair
	this.findCombination(vertex, result, visit, 0, 0, n, edge)
}
func main() {
	var task * Combinations = getCombinations()
	// Graph vertex
	// Its must be positive because it indicates index node id.
	// Assume that given vertex are distinct.
	var vertex = [] int {
		2,
		16,
		5,
		6,
		7,
		1,
	}
	// Get the number of nodes
	var n int = len(vertex)
	// number of edge
	var m int = 2
	/*
	    arr [] = [2, 16, 5, 6, 7, 1]
	    -----------------------------
	    2 ⤑ 6 ⤑ 7  (2&6&7) : 2
	    2 ⤑ 7 ⤑ 6  (2&7&6) : 2
	    5 ⤑ 6 ⤑ 7  (5&6&7) : 4
	    5 ⤑ 7 ⤑ 6  (5&7&6) : 4
	    5 ⤑ 7 ⤑ 1  (5&7&1) : 1
	    5 ⤑ 1 ⤑ 7  (5&1&7) : 1
	    6 ⤑ 2 ⤑ 7  (6&2&7) : 2
	    6 ⤑ 5 ⤑ 7  (6&5&7) : 4
	    6 ⤑ 7 ⤑ 2  (6&7&2) : 2
	    6 ⤑ 7 ⤑ 5  (6&7&5) : 4
	    7 ⤑ 2 ⤑ 6  (7&2&6) : 2
	    7 ⤑ 5 ⤑ 6  (7&5&6) : 4
	    7 ⤑ 5 ⤑ 1  (7&5&1) : 1
	    7 ⤑ 6 ⤑ 2  (7&6&2) : 2
	    7 ⤑ 6 ⤑ 5  (7&6&5) : 4
	    7 ⤑ 1 ⤑ 5  (7&1&5) : 1
	    1 ⤑ 5 ⤑ 7  (1&5&7) : 1
	    1 ⤑ 7 ⤑ 5  (1&7&5) : 1
	  -------------------------
	    Note m = 2 so m+1 node is possible in result
	*/
	task.combination(vertex, n, m)
}

Output

2 ⤑ 6 ⤑ 7 : 2
2 ⤑ 7 ⤑ 6 : 2
5 ⤑ 6 ⤑ 7 : 4
5 ⤑ 7 ⤑ 6 : 4
5 ⤑ 7 ⤑ 1 : 1
5 ⤑ 1 ⤑ 7 : 1
6 ⤑ 2 ⤑ 7 : 2
6 ⤑ 5 ⤑ 7 : 4
6 ⤑ 7 ⤑ 2 : 2
6 ⤑ 7 ⤑ 5 : 4
7 ⤑ 2 ⤑ 6 : 2
7 ⤑ 5 ⤑ 6 : 4
7 ⤑ 5 ⤑ 1 : 1
7 ⤑ 6 ⤑ 2 : 2
7 ⤑ 6 ⤑ 5 : 4
7 ⤑ 1 ⤑ 5 : 1
1 ⤑ 5 ⤑ 7 : 1
1 ⤑ 7 ⤑ 5 : 1
<?php
// Php Program
// Print the combinations of given vertices which connect of M edges
// they not contain cycle and its path cost is positive by node
class Combinations
{
	public	function findCombination($vertex, $result, 
                                      $visit, $cost, 
                                      $count, $n, $edge)
	{
		if ($count - 1 == $edge && $cost > 0)
		{
			for ($i = 0; $i <= $edge; ++$i)
			{
				if ($i != 0)
				{
					echo(" ⤑ ");
				}
				echo($result[$i]);
			}
			echo(" : ".$cost.
				"\n");
		}
		if ($count >= $n || $count - 1 >= $edge || 
            ($count >= 2 && $cost == 0))
		{
			// Base case
			return;
		}
		for ($i = 0; $i < $n; ++$i)
		{
			if ($visit[$i] == -1)
			{
				$visit[$i] = $i;
				// Collect resultant node
				$result[$count] = $vertex[$i];
				if ($count == 0)
				{
					// Find combinations using recursively
					$this->findCombination($vertex, 
                                           $result, $visit, 
                                           $vertex[$i], $count + 1, 
                                           $n, $edge);
				}
				else
				{
					// Find combinations using recursively
					$this->findCombination($vertex, $result, 
                                           $visit, $vertex[$i] & $cost, 
                                           $count + 1, $n, $edge);
				}
				$visit[$i] = -1;
			}
		}
	}
	public	function combination($vertex, $n, $edge)
	{
		if ($n <= 0 || $edge >= $n || $edge <= 0)
		{
			return;
		}
		$result = array_fill(0, $edge + 1, 0);
		$visit = array_fill(0, $n, 0);
		for ($i = 0; $i < $n; ++$i)
		{
			$visit[$i] = -1;
		}
		// Find combination pair
		$this->findCombination($vertex, $result, $visit, 
                               0, 0, $n, $edge);
	}
}

function main()
{
	$task = new Combinations();
	// Graph vertex
	// Its must be positive because it indicates index node id.
	// Assume that given vertex are distinct.
	$vertex = array(2, 16, 5, 6, 7, 1);
	// Get the number of nodes
	$n = count($vertex);
	// number of edge
	$m = 2;
	/*
	    arr [] = [2, 16, 5, 6, 7, 1]
	    -----------------------------
	    2 ⤑ 6 ⤑ 7  (2&6&7) : 2
	    2 ⤑ 7 ⤑ 6  (2&7&6) : 2
	    5 ⤑ 6 ⤑ 7  (5&6&7) : 4
	    5 ⤑ 7 ⤑ 6  (5&7&6) : 4
	    5 ⤑ 7 ⤑ 1  (5&7&1) : 1
	    5 ⤑ 1 ⤑ 7  (5&1&7) : 1
	    6 ⤑ 2 ⤑ 7  (6&2&7) : 2
	    6 ⤑ 5 ⤑ 7  (6&5&7) : 4
	    6 ⤑ 7 ⤑ 2  (6&7&2) : 2
	    6 ⤑ 7 ⤑ 5  (6&7&5) : 4
	    7 ⤑ 2 ⤑ 6  (7&2&6) : 2
	    7 ⤑ 5 ⤑ 6  (7&5&6) : 4
	    7 ⤑ 5 ⤑ 1  (7&5&1) : 1
	    7 ⤑ 6 ⤑ 2  (7&6&2) : 2
	    7 ⤑ 6 ⤑ 5  (7&6&5) : 4
	    7 ⤑ 1 ⤑ 5  (7&1&5) : 1
	    1 ⤑ 5 ⤑ 7  (1&5&7) : 1
	    1 ⤑ 7 ⤑ 5  (1&7&5) : 1
	  -------------------------
	    Note m = 2 so m+1 node is possible in result
	*/
	$task->combination($vertex, $n, $m);
}
main();

Output

2 ⤑ 6 ⤑ 7 : 2
2 ⤑ 7 ⤑ 6 : 2
5 ⤑ 6 ⤑ 7 : 4
5 ⤑ 7 ⤑ 6 : 4
5 ⤑ 7 ⤑ 1 : 1
5 ⤑ 1 ⤑ 7 : 1
6 ⤑ 2 ⤑ 7 : 2
6 ⤑ 5 ⤑ 7 : 4
6 ⤑ 7 ⤑ 2 : 2
6 ⤑ 7 ⤑ 5 : 4
7 ⤑ 2 ⤑ 6 : 2
7 ⤑ 5 ⤑ 6 : 4
7 ⤑ 5 ⤑ 1 : 1
7 ⤑ 6 ⤑ 2 : 2
7 ⤑ 6 ⤑ 5 : 4
7 ⤑ 1 ⤑ 5 : 1
1 ⤑ 5 ⤑ 7 : 1
1 ⤑ 7 ⤑ 5 : 1
// Node JS Program
// Print the combinations of given vertices which connect of M edges
// they not contain cycle and its path cost is positive by node
class Combinations
{
	findCombination(vertex, result, visit, cost, count, n, edge)
	{
		if (count - 1 == edge && cost > 0)
		{
			for (var i = 0; i <= edge; ++i)
			{
				if (i != 0)
				{
					process.stdout.write(" ⤑ ");
				}
				process.stdout.write("" + result[i]);
			}
			process.stdout.write(" : " + cost + "\n");
		}
		if (count >= n || count - 1 >= edge || (count >= 2 && cost == 0))
		{
			// Base case
			return;
		}
		for (var i = 0; i < n; ++i)
		{
			if (visit[i] == -1)
			{
				visit[i] = i;
				// Collect resultant node
				result[count] = vertex[i];
				if (count == 0)
				{
					// Find combinations using recursively
					this.findCombination(vertex, result, 
                                         visit, vertex[i], 
                                         count + 1, n, edge);
				}
				else
				{
					// Find combinations using recursively
					this.findCombination(vertex, result, 
                                         visit, vertex[i] & cost, 
                                         count + 1, n, edge);
				}
				visit[i] = -1;
			}
		}
	}
	combination(vertex, n, edge)
	{
		if (n <= 0 || edge >= n || edge <= 0)
		{
			return;
		}
		var result = Array(edge + 1).fill(0);
		var visit = Array(n).fill(0);
		for (var i = 0; i < n; ++i)
		{
			visit[i] = -1;
		}
		// Find combination pair
		this.findCombination(vertex, result, visit, 0, 0, n, edge);
	}
}

function main()
{
	var task = new Combinations();
	// Graph vertex
	// Its must be positive because it indicates index node id.
	// Assume that given vertex are distinct.
	var vertex = [2, 16, 5, 6, 7, 1];
	// Get the number of nodes
	var n = vertex.length;
	// number of edge
	var m = 2;
	/*
	    arr [] = [2, 16, 5, 6, 7, 1]
	    -----------------------------
	    2 ⤑ 6 ⤑ 7  (2&6&7) : 2
	    2 ⤑ 7 ⤑ 6  (2&7&6) : 2
	    5 ⤑ 6 ⤑ 7  (5&6&7) : 4
	    5 ⤑ 7 ⤑ 6  (5&7&6) : 4
	    5 ⤑ 7 ⤑ 1  (5&7&1) : 1
	    5 ⤑ 1 ⤑ 7  (5&1&7) : 1
	    6 ⤑ 2 ⤑ 7  (6&2&7) : 2
	    6 ⤑ 5 ⤑ 7  (6&5&7) : 4
	    6 ⤑ 7 ⤑ 2  (6&7&2) : 2
	    6 ⤑ 7 ⤑ 5  (6&7&5) : 4
	    7 ⤑ 2 ⤑ 6  (7&2&6) : 2
	    7 ⤑ 5 ⤑ 6  (7&5&6) : 4
	    7 ⤑ 5 ⤑ 1  (7&5&1) : 1
	    7 ⤑ 6 ⤑ 2  (7&6&2) : 2
	    7 ⤑ 6 ⤑ 5  (7&6&5) : 4
	    7 ⤑ 1 ⤑ 5  (7&1&5) : 1
	    1 ⤑ 5 ⤑ 7  (1&5&7) : 1
	    1 ⤑ 7 ⤑ 5  (1&7&5) : 1
	  -------------------------
	    Note m = 2 so m+1 node is possible in result
	*/
	task.combination(vertex, n, m);
}
main();

Output

2 ⤑ 6 ⤑ 7 : 2
2 ⤑ 7 ⤑ 6 : 2
5 ⤑ 6 ⤑ 7 : 4
5 ⤑ 7 ⤑ 6 : 4
5 ⤑ 7 ⤑ 1 : 1
5 ⤑ 1 ⤑ 7 : 1
6 ⤑ 2 ⤑ 7 : 2
6 ⤑ 5 ⤑ 7 : 4
6 ⤑ 7 ⤑ 2 : 2
6 ⤑ 7 ⤑ 5 : 4
7 ⤑ 2 ⤑ 6 : 2
7 ⤑ 5 ⤑ 6 : 4
7 ⤑ 5 ⤑ 1 : 1
7 ⤑ 6 ⤑ 2 : 2
7 ⤑ 6 ⤑ 5 : 4
7 ⤑ 1 ⤑ 5 : 1
1 ⤑ 5 ⤑ 7 : 1
1 ⤑ 7 ⤑ 5 : 1
#  Python 3 Program
#  Print the combinations of given vertices which connect of M edges
#  they not contain cycle and its path cost is positive by node
class Combinations :
	def findCombination(self, vertex, result, 
                        visit, cost, count, n, edge) :
		if (count - 1 == edge and cost > 0) :
			i = 0
			while (i <= edge) :
				if (i != 0) :
					print(" ⤑ ", end = "")
				
				print(result[i], end = "")
				i += 1
			
			print(" : ", cost )
		
		if (count >= n or count - 1 >= edge or (count >= 2 and cost == 0)) :
			#  Base case
			return
		
		i = 0
		while (i < n) :
			if (visit[i] == -1) :
				visit[i] = i
				#  Collect resultant node
				result[count] = vertex[i]
				if (count == 0) :
					#  Find combinations using recursively
					self.findCombination(vertex, result, 
                                         visit, vertex[i], 
                                         count + 1, n, edge)
				else :
					#  Find combinations using recursively
					self.findCombination(vertex, result, 
                                         visit, vertex[i] & cost, 
                                         count + 1, n, edge)
				
				visit[i] = -1
			
			i += 1
		
	
	def combination(self, vertex, n, edge) :
		if (n <= 0 or edge >= n or edge <= 0) :
			return
		
		result = [0] * (edge + 1)
		visit = [0] * (n)
		i = 0
		while (i < n) :
			visit[i] = -1
			i += 1
		
		#  Find combination pair
		self.findCombination(vertex, result, visit, 0, 0, n, edge)
	

def main() :
	task = Combinations()
	#  Graph vertex
	#  Its must be positive because it indicates index node id.
	#  Assume that given vertex are distinct.
	vertex = [2, 16, 5, 6, 7, 1]
	#  Get the number of nodes
	n = len(vertex)
	#  number of edge
	m = 2
	#    arr [] = [2, 16, 5, 6, 7, 1]
	#    -----------------------------
	#    2 ⤑ 6 ⤑ 7  (2&6&7) : 2
	#    2 ⤑ 7 ⤑ 6  (2&7&6) : 2
	#    5 ⤑ 6 ⤑ 7  (5&6&7) : 4
	#    5 ⤑ 7 ⤑ 6  (5&7&6) : 4
	#    5 ⤑ 7 ⤑ 1  (5&7&1) : 1
	#    5 ⤑ 1 ⤑ 7  (5&1&7) : 1
	#    6 ⤑ 2 ⤑ 7  (6&2&7) : 2
	#    6 ⤑ 5 ⤑ 7  (6&5&7) : 4
	#    6 ⤑ 7 ⤑ 2  (6&7&2) : 2
	#    6 ⤑ 7 ⤑ 5  (6&7&5) : 4
	#    7 ⤑ 2 ⤑ 6  (7&2&6) : 2
	#    7 ⤑ 5 ⤑ 6  (7&5&6) : 4
	#    7 ⤑ 5 ⤑ 1  (7&5&1) : 1
	#    7 ⤑ 6 ⤑ 2  (7&6&2) : 2
	#    7 ⤑ 6 ⤑ 5  (7&6&5) : 4
	#    7 ⤑ 1 ⤑ 5  (7&1&5) : 1
	#    1 ⤑ 5 ⤑ 7  (1&5&7) : 1
	#    1 ⤑ 7 ⤑ 5  (1&7&5) : 1
	#  -------------------------
	#    Note m = 2 so m+1 node is possible in result
	task.combination(vertex, n, m)

if __name__ == "__main__": main()

Output

2 ⤑ 6 ⤑ 7 :  2
2 ⤑ 7 ⤑ 6 :  2
5 ⤑ 6 ⤑ 7 :  4
5 ⤑ 7 ⤑ 6 :  4
5 ⤑ 7 ⤑ 1 :  1
5 ⤑ 1 ⤑ 7 :  1
6 ⤑ 2 ⤑ 7 :  2
6 ⤑ 5 ⤑ 7 :  4
6 ⤑ 7 ⤑ 2 :  2
6 ⤑ 7 ⤑ 5 :  4
7 ⤑ 2 ⤑ 6 :  2
7 ⤑ 5 ⤑ 6 :  4
7 ⤑ 5 ⤑ 1 :  1
7 ⤑ 6 ⤑ 2 :  2
7 ⤑ 6 ⤑ 5 :  4
7 ⤑ 1 ⤑ 5 :  1
1 ⤑ 5 ⤑ 7 :  1
1 ⤑ 7 ⤑ 5 :  1
#  Ruby Program
#  Print the combinations of given vertices which connect of M edges
#  they not contain cycle and its path cost is positive by node
class Combinations 
	def findCombination(vertex, result, visit, 
                        cost, count, n, edge) 
		if (count - 1 == edge && cost > 0) 
			i = 0
			while (i <= edge) 
				if (i != 0) 
					print(" ⤑ ")
				end

				print(result[i])
				i += 1
			end

			print(" : ", cost ,"\n")
		end

		if (count >= n || count - 1 >= edge || 
            (count >= 2 && cost == 0)) 
			#  Base case
			return
		end

		i = 0
		while (i < n) 
			if (visit[i] == -1) 
				visit[i] = i
				#  Collect resultant node
				result[count] = vertex[i]
				if (count == 0) 
					#  Find combinations using recursively
					self.findCombination(vertex, result, 
                                         visit, vertex[i], 
                                         count + 1, n, edge)
				else
 
					#  Find combinations using recursively
					self.findCombination(vertex, result, 
                                         visit, vertex[i] & cost, 
                                         count + 1, n, edge)
				end

				visit[i] = -1
			end

			i += 1
		end

	end

	def combination(vertex, n, edge) 
		if (n <= 0 || edge >= n || edge <= 0) 
			return
		end

		result = Array.new(edge + 1) {0}
		visit = Array.new(n) {0}
		i = 0
		while (i < n) 
			visit[i] = -1
			i += 1
		end

		#  Find combination pair
		self.findCombination(vertex, result, visit, 0, 0, n, edge)
	end

end

def main() 
	task = Combinations.new()
	#  Graph vertex
	#  Its must be positive because it indicates index node id.
	#  Assume that given vertex are distinct.
	vertex = [2, 16, 5, 6, 7, 1]
	#  Get the number of nodes
	n = vertex.length
	#  number of edge
	m = 2
	#    arr [] = [2, 16, 5, 6, 7, 1]
	#    -----------------------------
	#    2 ⤑ 6 ⤑ 7  (2&6&7) : 2
	#    2 ⤑ 7 ⤑ 6  (2&7&6) : 2
	#    5 ⤑ 6 ⤑ 7  (5&6&7) : 4
	#    5 ⤑ 7 ⤑ 6  (5&7&6) : 4
	#    5 ⤑ 7 ⤑ 1  (5&7&1) : 1
	#    5 ⤑ 1 ⤑ 7  (5&1&7) : 1
	#    6 ⤑ 2 ⤑ 7  (6&2&7) : 2
	#    6 ⤑ 5 ⤑ 7  (6&5&7) : 4
	#    6 ⤑ 7 ⤑ 2  (6&7&2) : 2
	#    6 ⤑ 7 ⤑ 5  (6&7&5) : 4
	#    7 ⤑ 2 ⤑ 6  (7&2&6) : 2
	#    7 ⤑ 5 ⤑ 6  (7&5&6) : 4
	#    7 ⤑ 5 ⤑ 1  (7&5&1) : 1
	#    7 ⤑ 6 ⤑ 2  (7&6&2) : 2
	#    7 ⤑ 6 ⤑ 5  (7&6&5) : 4
	#    7 ⤑ 1 ⤑ 5  (7&1&5) : 1
	#    1 ⤑ 5 ⤑ 7  (1&5&7) : 1
	#    1 ⤑ 7 ⤑ 5  (1&7&5) : 1
	#  -------------------------
	#    Note m = 2 so m+1 node is possible in result
	task.combination(vertex, n, m)
end

main()

Output

2 ⤑ 6 ⤑ 7 : 2
2 ⤑ 7 ⤑ 6 : 2
5 ⤑ 6 ⤑ 7 : 4
5 ⤑ 7 ⤑ 6 : 4
5 ⤑ 7 ⤑ 1 : 1
5 ⤑ 1 ⤑ 7 : 1
6 ⤑ 2 ⤑ 7 : 2
6 ⤑ 5 ⤑ 7 : 4
6 ⤑ 7 ⤑ 2 : 2
6 ⤑ 7 ⤑ 5 : 4
7 ⤑ 2 ⤑ 6 : 2
7 ⤑ 5 ⤑ 6 : 4
7 ⤑ 5 ⤑ 1 : 1
7 ⤑ 6 ⤑ 2 : 2
7 ⤑ 6 ⤑ 5 : 4
7 ⤑ 1 ⤑ 5 : 1
1 ⤑ 5 ⤑ 7 : 1
1 ⤑ 7 ⤑ 5 : 1
// Scala Program
// Print the combinations of given vertices which connect of M edges
// they not contain cycle and its path cost is positive by node
class Combinations()
{
	def findCombination(vertex: Array[Int], 
      result: Array[Int], visit: Array[Int], 
        cost: Int, count: Int, n: Int, edge: Int): Unit = {
		if (count - 1 == edge && cost > 0)
		{
			var i: Int = 0;
			while (i <= edge)
			{
				if (i != 0)
				{
					print(" ⤑ ");
				}
				print(result(i));
				i += 1;
			}
			print(" : " + cost + "\n");
		}
		if (count >= n || count - 1 >= edge || 
          (count >= 2 && cost == 0))
		{
			// Base case
			return;
		}
		var i: Int = 0;
		while (i < n)
		{
			if (visit(i) == -1)
			{
				visit(i) = i;
				// Collect resultant node
				result(count) = vertex(i);
				if (count == 0)
				{
					// Find combinations using recursively
					findCombination(vertex, result, 
                                    visit, vertex(i), count + 1, 
                                    n, edge);
				}
				else
				{
					// Find combinations using recursively
					findCombination(vertex, result, 
                                    visit, vertex(i) & cost, 
                                    count + 1, n, edge);
				}
				visit(i) = -1;
			}
			i += 1;
		}
	}
	def combination(vertex: Array[Int], n: Int, edge: Int): Unit = {
		if (n <= 0 || edge >= n || edge <= 0)
		{
			return;
		}
		var result: Array[Int] = Array.fill[Int](edge + 1)(0);
		var visit: Array[Int] = Array.fill[Int](n)(0);
		var i: Int = 0;
		while (i < n)
		{
			visit(i) = -1;
			i += 1;
		}
		// Find combination pair
		findCombination(vertex, result, visit, 0, 0, n, edge);
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Combinations = new Combinations();
		// Graph vertex
		// Its must be positive because it indicates index node id.
		// Assume that given vertex are distinct.
		var vertex: Array[Int] = Array(2, 16, 5, 6, 7, 1);
		// Get the number of nodes
		var n: Int = vertex.length;
		// number of edge
		var m: Int = 2;
		/*
		    arr [] = [2, 16, 5, 6, 7, 1]
		    -----------------------------
		    2 ⤑ 6 ⤑ 7  (2&6&7) : 2
		    2 ⤑ 7 ⤑ 6  (2&7&6) : 2
		    5 ⤑ 6 ⤑ 7  (5&6&7) : 4
		    5 ⤑ 7 ⤑ 6  (5&7&6) : 4
		    5 ⤑ 7 ⤑ 1  (5&7&1) : 1
		    5 ⤑ 1 ⤑ 7  (5&1&7) : 1
		    6 ⤑ 2 ⤑ 7  (6&2&7) : 2
		    6 ⤑ 5 ⤑ 7  (6&5&7) : 4
		    6 ⤑ 7 ⤑ 2  (6&7&2) : 2
		    6 ⤑ 7 ⤑ 5  (6&7&5) : 4
		    7 ⤑ 2 ⤑ 6  (7&2&6) : 2
		    7 ⤑ 5 ⤑ 6  (7&5&6) : 4
		    7 ⤑ 5 ⤑ 1  (7&5&1) : 1
		    7 ⤑ 6 ⤑ 2  (7&6&2) : 2
		    7 ⤑ 6 ⤑ 5  (7&6&5) : 4
		    7 ⤑ 1 ⤑ 5  (7&1&5) : 1
		    1 ⤑ 5 ⤑ 7  (1&5&7) : 1
		    1 ⤑ 7 ⤑ 5  (1&7&5) : 1
		  -------------------------
		    Note m = 2 so m+1 node is possible in result
		*/
		task.combination(vertex, n, m);
	}
}

Output

2 ⤑ 6 ⤑ 7 : 2
2 ⤑ 7 ⤑ 6 : 2
5 ⤑ 6 ⤑ 7 : 4
5 ⤑ 7 ⤑ 6 : 4
5 ⤑ 7 ⤑ 1 : 1
5 ⤑ 1 ⤑ 7 : 1
6 ⤑ 2 ⤑ 7 : 2
6 ⤑ 5 ⤑ 7 : 4
6 ⤑ 7 ⤑ 2 : 2
6 ⤑ 7 ⤑ 5 : 4
7 ⤑ 2 ⤑ 6 : 2
7 ⤑ 5 ⤑ 6 : 4
7 ⤑ 5 ⤑ 1 : 1
7 ⤑ 6 ⤑ 2 : 2
7 ⤑ 6 ⤑ 5 : 4
7 ⤑ 1 ⤑ 5 : 1
1 ⤑ 5 ⤑ 7 : 1
1 ⤑ 7 ⤑ 5 : 1
import Foundation;
// Swift 4 Program
// Print the combinations of given vertices which connect of M edges
// they not contain cycle and its path cost is positive by node
class Combinations
{
	func findCombination(_ vertex: [Int], 
  _ result: inout[Int], _ visit: inout[Int], _ cost: Int, 
    _ count: Int, _ n: Int, _ edge: Int)
	{
		if (count - 1 == edge && cost > 0)
		{
			var i: Int = 0;
			while (i <= edge)
			{
				if (i  != 0)
				{
					print(" ⤑ ", terminator: "");
				}
				print(result[i], terminator: "");
				i += 1;
			}
			print(" : ", cost );
		}
		if (count >= n || count - 1 >= edge || 
            (count >= 2 && cost == 0))
		{
			// Base case
			return;
		}
		var i: Int = 0;
		while (i < n)
		{
			if (visit[i] == -1)
			{
				visit[i] = i;
				// Collect resultant node
				result[count] = vertex[i];
				if (count == 0)
				{
					// Find combinations using recursively
					self.findCombination(vertex, &result, &visit, 
                                         vertex[i], count + 1, n, edge);
				}
				else
				{
					// Find combinations using recursively
					self.findCombination(vertex, &result, 
                                         &visit, vertex[i] & cost, 
                                         count + 1, n, edge);
				}
				visit[i] = -1;
			}
			i += 1;
		}
	}
	func combination(_ vertex: [Int], _ n: Int, _ edge: Int)
	{
		if (n <= 0 || edge >= n || edge <= 0)
		{
			return;
		}
		var result: [Int] = Array(repeating: 0, count: edge + 1);
		var visit: [Int] = Array(repeating: 0, count: n);
		var i: Int = 0;
		while (i < n)
		{
			visit[i] = -1;
			i += 1;
		}
		// Find combination pair
		self.findCombination(vertex, &result, &visit, 0, 0, n, edge);
	}
}
func main()
{
	let task: Combinations = Combinations();
	// Graph vertex
	// Its must be positive because it indicates index node id.
	// Assume that given vertex are distinct.
	let vertex: [Int] = [2, 16, 5, 6, 7, 1];
	// Get the number of nodes
	let n: Int = vertex.count;
	// number of edge
	let m: Int = 2;
	/*
	    arr [] = [2, 16, 5, 6, 7, 1]
	    -----------------------------
	    2 ⤑ 6 ⤑ 7  (2&6&7) : 2
	    2 ⤑ 7 ⤑ 6  (2&7&6) : 2
	    5 ⤑ 6 ⤑ 7  (5&6&7) : 4
	    5 ⤑ 7 ⤑ 6  (5&7&6) : 4
	    5 ⤑ 7 ⤑ 1  (5&7&1) : 1
	    5 ⤑ 1 ⤑ 7  (5&1&7) : 1
	    6 ⤑ 2 ⤑ 7  (6&2&7) : 2
	    6 ⤑ 5 ⤑ 7  (6&5&7) : 4
	    6 ⤑ 7 ⤑ 2  (6&7&2) : 2
	    6 ⤑ 7 ⤑ 5  (6&7&5) : 4
	    7 ⤑ 2 ⤑ 6  (7&2&6) : 2
	    7 ⤑ 5 ⤑ 6  (7&5&6) : 4
	    7 ⤑ 5 ⤑ 1  (7&5&1) : 1
	    7 ⤑ 6 ⤑ 2  (7&6&2) : 2
	    7 ⤑ 6 ⤑ 5  (7&6&5) : 4
	    7 ⤑ 1 ⤑ 5  (7&1&5) : 1
	    1 ⤑ 5 ⤑ 7  (1&5&7) : 1
	    1 ⤑ 7 ⤑ 5  (1&7&5) : 1
	  -------------------------
	    Note m = 2 so m+1 node is possible in result
	*/
	task.combination(vertex, n, m);
}
main();

Output

2 ⤑ 6 ⤑ 7 :  2
2 ⤑ 7 ⤑ 6 :  2
5 ⤑ 6 ⤑ 7 :  4
5 ⤑ 7 ⤑ 6 :  4
5 ⤑ 7 ⤑ 1 :  1
5 ⤑ 1 ⤑ 7 :  1
6 ⤑ 2 ⤑ 7 :  2
6 ⤑ 5 ⤑ 7 :  4
6 ⤑ 7 ⤑ 2 :  2
6 ⤑ 7 ⤑ 5 :  4
7 ⤑ 2 ⤑ 6 :  2
7 ⤑ 5 ⤑ 6 :  4
7 ⤑ 5 ⤑ 1 :  1
7 ⤑ 6 ⤑ 2 :  2
7 ⤑ 6 ⤑ 5 :  4
7 ⤑ 1 ⤑ 5 :  1
1 ⤑ 5 ⤑ 7 :  1
1 ⤑ 7 ⤑ 5 :  1
// Kotlin Program
// Print the combinations of given vertices which connect of M edges
// they not contain cycle and its path cost is positive by node
class Combinations
{
	fun findCombination(vertex: Array < Int > , 
                         result: Array < Int > , 
                         visit: Array < Int > , 
                         cost: Int, count: Int, 
                         n: Int, edge: Int): Unit
	{
		if (count - 1 == edge && cost > 0)
		{
			var i: Int = 0;
			while (i <= edge)
			{
				if (i != 0)
				{
					print(" ⤑ ");
				}
				print(result[i]);
				i += 1;
			}
			print(" : " + cost + "\n");
		}
		if (count >= n || count - 1 >= edge || 
            (count >= 2 && cost == 0))
		{
			// Base case
			return;
		}
		var i: Int = 0;
		while (i < n)
		{
			if (visit[i] == -1)
			{
				visit[i] = i;
				// Collect resultant node
				result[count] = vertex[i];
				if (count == 0)
				{
					// Find combinations using recursively
					this.findCombination(vertex, 
                                         result, visit, vertex[i], 
                                         count + 1, n, edge);
				}
				else
				{
					// Find combinations using recursively
					this.findCombination(vertex, result, 
                                         visit, vertex[i] and cost, 
                                         count + 1, n, edge);
				}
				visit[i] = -1;
			}
			i += 1;
		}
	}
	fun combination(vertex: Array < Int > , n: Int, edge: Int): Unit
	{
		if (n <= 0 || edge >= n || edge <= 0)
		{
			return;
		}
		val result: Array < Int > = Array(edge + 1)
		{
			0
		};
		val visit: Array < Int > = Array(n)
		{
			0
		};
		var i: Int = 0;
		while (i < n)
		{
			visit[i] = -1;
			i += 1;
		}
		// Find combination pair
		this.findCombination(vertex, result, visit, 0, 0, n, edge);
	}
}
fun main(args: Array < String > ): Unit
{
	val task: Combinations = Combinations();
	// Graph vertex
	// Its must be positive because it indicates index node id.
	// Assume that given vertex are distinct.
	val vertex: Array < Int > = arrayOf(2, 16, 5, 6, 7, 1);
	// Get the number of nodes
	val n: Int = vertex.count();
	// number of edge
	val m: Int = 2;
	/*
	    arr [] = [2, 16, 5, 6, 7, 1]
	    -----------------------------
	    2 ⤑ 6 ⤑ 7  (2&6&7) : 2
	    2 ⤑ 7 ⤑ 6  (2&7&6) : 2
	    5 ⤑ 6 ⤑ 7  (5&6&7) : 4
	    5 ⤑ 7 ⤑ 6  (5&7&6) : 4
	    5 ⤑ 7 ⤑ 1  (5&7&1) : 1
	    5 ⤑ 1 ⤑ 7  (5&1&7) : 1
	    6 ⤑ 2 ⤑ 7  (6&2&7) : 2
	    6 ⤑ 5 ⤑ 7  (6&5&7) : 4
	    6 ⤑ 7 ⤑ 2  (6&7&2) : 2
	    6 ⤑ 7 ⤑ 5  (6&7&5) : 4
	    7 ⤑ 2 ⤑ 6  (7&2&6) : 2
	    7 ⤑ 5 ⤑ 6  (7&5&6) : 4
	    7 ⤑ 5 ⤑ 1  (7&5&1) : 1
	    7 ⤑ 6 ⤑ 2  (7&6&2) : 2
	    7 ⤑ 6 ⤑ 5  (7&6&5) : 4
	    7 ⤑ 1 ⤑ 5  (7&1&5) : 1
	    1 ⤑ 5 ⤑ 7  (1&5&7) : 1
	    1 ⤑ 7 ⤑ 5  (1&7&5) : 1
	  -------------------------
	    Note m = 2 so m+1 node is possible in result
	*/
	task.combination(vertex, n, m);
}

Output

2 ⤑ 6 ⤑ 7 : 2
2 ⤑ 7 ⤑ 6 : 2
5 ⤑ 6 ⤑ 7 : 4
5 ⤑ 7 ⤑ 6 : 4
5 ⤑ 7 ⤑ 1 : 1
5 ⤑ 1 ⤑ 7 : 1
6 ⤑ 2 ⤑ 7 : 2
6 ⤑ 5 ⤑ 7 : 4
6 ⤑ 7 ⤑ 2 : 2
6 ⤑ 7 ⤑ 5 : 4
7 ⤑ 2 ⤑ 6 : 2
7 ⤑ 5 ⤑ 6 : 4
7 ⤑ 5 ⤑ 1 : 1
7 ⤑ 6 ⤑ 2 : 2
7 ⤑ 6 ⤑ 5 : 4
7 ⤑ 1 ⤑ 5 : 1
1 ⤑ 5 ⤑ 7 : 1
1 ⤑ 7 ⤑ 5 : 1


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved