Untitled

 avatar
unknown
plain_text
3 years ago
7.2 kB
7
Indexable
/*import java.util.InputMismatchException;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;*/
import java.util.*; 

public class BFS
{ 
 
    private Queue<Integer> queue;
 
    public BFS()
    {
        queue = new LinkedList<Integer>();
    }
 
    public void bfs(int adjacency_matrix[][], int source)
    {
        int number_of_nodes = adjacency_matrix[source].length - 1;
 
        int[] visited = new int[number_of_nodes + 1];
        int i, element;
 
        visited[source] = 1;
        queue.add(source);
 
        while (!queue.isEmpty())
        {
            element = queue.remove();
            i = element;
            System.out.print(i + "\t");
            while (i <= number_of_nodes)
            {
                if (adjacency_matrix[element][i] == 1 && visited[i] == 0)
                {
                    queue.add(i);
                    visited[i] = 1;
                }
                i++;
            }
        }
    }
 
    public static void main(String arg[])
    {
        int number_no_nodes, source;
        Scanner scanner = null;
 
        try
        {
            System.out.println("Enter the number of nodes in the graph");
            scanner = new Scanner(System.in);
            number_no_nodes = scanner.nextInt();
 
            int adjacency_matrix[][] = new int[number_no_nodes + 1][number_no_nodes + 1];
            System.out.println("Enter the adjacency matrix");
            for (int i = 1; i <= number_no_nodes; i++)
                for (int j = 1; j <= number_no_nodes; j++)
                    adjacency_matrix[i][j] = scanner.nextInt();
 
            System.out.println("Enter the source for the graph");
            source = scanner.nextInt();
 
            System.out.println("The BFS traversal of the graph is ");
            BFS bfs = new BFS();
            bfs.bfs(adjacency_matrix, source);
 
        } catch (InputMismatchException inputMismatch)
        {
            System.out.println("Wrong Input Format");
        }
        scanner.close();
    }
}




/*import java.util.InputMismatchException;
import java.util.Scanner;
import java.util.Stack;*/
import java.util.*;
 
public class DFS
{
    private Stack<Integer> stack;
 
    public DFS() 
    {
        stack = new Stack<Integer>();
    }
 
    public void dfs(int adjacency_matrix[][], int source)
    {
        int number_of_nodes = adjacency_matrix[source].length - 1;
 
        int visited[] = new int[number_of_nodes + 1];		
        int element = source;		
        int i = source;		
        System.out.print(element + "\t");		
        visited[source] = 1;		
        stack.push(source);
 
        while (!stack.isEmpty())
        {
            element = stack.peek();
            i = element;	
	    while (i <= number_of_nodes)
	    {
     	        if (adjacency_matrix[element][i] == 1 && visited[i] == 0)
	        {
                    stack.push(i);
                    visited[i] = 1;
                    element = i;
                    i = 1;
                    System.out.print(element + "\t");
	            continue;
                }
                i++;
	    }
            stack.pop();	
        }	
    }
 
    public static void main(String args[])
    {
        int number_of_nodes, source;
        Scanner scanner = null;
 	try
        {
	    System.out.println("Enter the number of nodes in the graph");
            scanner = new Scanner(System.in);
            number_of_nodes = scanner.nextInt();
 
	    int adjacency_matrix[][] = new int[number_of_nodes + 1][number_of_nodes + 1];
	    System.out.println("Enter the adjacency matrix");
	    for (int i = 1; i <= number_of_nodes; i++)
	        for (int j = 1; j <= number_of_nodes; j++)
                    adjacency_matrix[i][j] = scanner.nextInt();
 
	    System.out.println("Enter the source for the graph");
            source = scanner.nextInt(); 
 
            System.out.println("The DFS Traversal for the graph is given by ");
            DFS dfs = new DFS();
            dfs.dfs(adjacency_matrix, source);					
        }catch(InputMismatchException inputMismatch)
        {
            System.out.println("Wrong Input format");
        }	
        scanner.close();	
    }	
}



import java.util.*;
public class EightQueens 
{
  	public static void main(String args[]) 
	{
    		Scanner src=new Scanner (System.in);
		System.out.println ("Enter the number of queens you want to place :");
		int  N=src.nextInt();
		int[][] board = new int[N][N];
    		solve(0, board, N);

		for (int i = 0; i < N; i++) 
		{
			for (int j = 0; j < N; j++) 
			{
				if (board[i][j]==1) 
    				System.out.print ("Q ");
				else 
				System.out.print("* ");
      			}
      			System.out.println();
    		}
	  }

	static boolean solve(int row, int[][] board, int N) 
	{
    		if(row>=N) return true;
    		for(int position = 0; position < N; position++) 
		{
      			if(isValid(board, row, position, N)) 
			{
			        board[row][position] = 1;
			        if(!solve(row+1, board, N)) 
				{
          				board[row][position] = 0;
        			}
				else
			        return true;
      			}
    		}
    		return false;
  	}

  	static boolean isValid(int[][] board, int x, int y, int N) 
	{
    		int i, j;
    		for(i = 0; i < x; i++)
      			if(board[i][y]==1)
        		return false;
    		i = x - 1;
    		j = y - 1;
    		while((i>=0)&&(j>=0))
      			if(board[i--][j--]==1) 
			return false;
    		i = x - 1;
    		j = y + 1;
    		while((i>=0)&&(j<N))
      			if(board[i--][j++]==1) 
			return false;
    		return true;
  	}
}


import java.util.Scanner;
public class Minimax 
{

   	public static int min(int a[][],int n,int setIndex) 
   	{
        	int smallest = a[setIndex][0];
	        for(int i=1; i<n; i++) 
		{
	        	if(smallest > a[setIndex][i])
        	        smallest = a[setIndex][i];
        	}
        	return smallest;
    	}

	public static int max(int a[][],int n,int setIndex) 
	{
        	int greatest = a[setIndex][0];
	        for(int i=1; i<n; i++) 
		{
        		if(greatest < a[setIndex][i])
	                greatest = a[setIndex][i];
        	}
	        return greatest;
    	}

	public static void main(String[] args) 
	{
       		Scanner s = new Scanner(System.in);
	        System.out.println("Enter the no. of nodes in each subtree");
        	int n = s.nextInt();
	        int set[][] = new int[n][n];
        	System.out.println("Enter the utility values: ");
	        for(int i=0; i<n; i++) 
		{
	            	for(int j=0; j<n; j++) 
			{
                		set[i][j] = s.nextInt();
            		}
        	}
	
	        int max[][] = new int[1][n];
        	System.out.print("The min values retured are: ");
	        for(int i=0; i<n; i++) 
		{
	            max[0][i] = min(set, n, i);
        	    System.out.print(" " +max[0][i]);
        	}

	        System.out.println("");
        	int maxValue = max(max, n, 0);
	        System.out.println("The Max Value is " + maxValue);
    	}
}
Editor is loading...