0% found this document useful (0 votes)
25 views7 pages

VERTEXCOVER

VERTEXCOVER
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views7 pages

VERTEXCOVER

VERTEXCOVER
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

VERTEXCOVER

public class VertexCover {

static int min(int x, int y)

return (x < y) ? x : y;

static class node

int data;

node left, right;

};

static int vCover(node root)

if (root == null)

return 0;

if (root.left == null && root.right == null)

return 0;

int size_incl = 1 + vCover(root.left) +

vCover(root.right);

int size_excl = 0;

if (root.left != null)
size_excl += 1 + vCover(root.left.left) +

vCover(root.left.right);

if (root.right != null)

size_excl += 1 + vCover(root.right.left) +

vCover(root.right.right);

return Math.min(size_incl, size_excl);

static node newNode(int data)

node temp = new node();

temp.data = data;

temp.left = temp.right = null;

return temp;

public static void main(String[] args)

node root = newNode(1);

root.left = newNode(2);

root.left.left = newNode(1);

root.left.right = newNode(4);

root.left.right.left = newNode(2);

root.left.right.right = newNode(5);

root.left.right.right.left = newNode(4);

root.left.right.right.right = newNode(8);
root.right = newNode(3);

root.right.right = newNode(6);

root.right.right.left = newNode(3);

root.right.right.right = newNode(7);

root.right.right.right.left = newNode(6);

root.right.right.right.right = newNode(8);

root.right.left = newNode(1);

System.out.printf("Tamaño del vertice mas pequeño" +

" %d ", vCover(root));

FERMAT

public class Fermat {

public static boolean Fermat(long base, long mod)

long exp = mod-1;

System.out.println("\na es: " + base);

System.out.println("n es: " + mod);

System.out.println("n-1 es: " + exp);

System.out.println("a^n-1 es: " + Math.pow(base,mod));

if((Math.pow(base, exp))%mod == 1)

return true;

else

{
return false;

public static void main(String[] args) {

System.out.println("La solución es un número primo: " +

Fermat(5,7) + "\n");

SETCOVER

import java.io.IOException;

import java.util.ArrayList;

import java.util.Arrays;

import java.util.Collections;

import java.util.Comparator;

import java.util.LinkedHashSet;

import java.util.List;

import java.util.Set;

/*

* To change this license header, choose License Headers in Project Properties.

* To change this template file, choose Tools | Templates

* and open the template in the editor.

*/

/**

* @author USER HP
*/

public class SetCover {

interface Filter<T> {

boolean matches(T t);

public static void main(String... args) throws IOException {

Integer[][] arrayOfSets = {

{1,2,4},

{2,3,4},

{4,5},

{3,4,5},

};

Integer[] solution = {1,2,3,4,5};

List<Set<Integer>> listOfSets = new ArrayList<Set<Integer>>();

for (Integer[] array : arrayOfSets)

listOfSets.add(new LinkedHashSet<Integer>(Arrays.asList(array)));

final Set<Integer> solutionSet = new LinkedHashSet<Integer>(Arrays.asList(solution));

Filter<Set<Set<Integer>>> filter = new Filter<Set<Set<Integer>>>() {

public boolean matches(Set<Set<Integer>> integers) {

Set<Integer> union = new LinkedHashSet<Integer>();

for (Set<Integer> ints : integers)

union.addAll(ints);
return union.equals(solutionSet);

};

Set<Set<Integer>> firstSolution = shortestCombination(filter, listOfSets);

System.out.println("La combinacion mas corta "+firstSolution);

private static <T> Set<T> shortestCombination(Filter<Set<T>> filter, List<T> listOfSets) {

final int size = listOfSets.size();

if (size > 20) throw new IllegalArgumentException("muchas combinaciones");

int combinations = 1 << size;

List<Set<T>> possibleSolutions = new ArrayList<Set<T>>();

for(int l = 0;l<combinations;l++) {

Set<T> combination = new LinkedHashSet<T>();

for(int j=0;j<size;j++) {

if (((l >> j) & 1) != 0)

combination.add(listOfSets.get(j));

possibleSolutions.add(combination);

Collections.sort(possibleSolutions, new Comparator<Set<T>>() {

public int compare(Set<T> o1, Set<T> o2) {

return o1.size()-o2.size();

});

for (Set<T> possibleSolution : possibleSolutions) {

if (filter.matches(possibleSolution))

return possibleSolution;

}
return null;

You might also like