












































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
Lab Manual for Artificial Intelligence
Typology: Lab Reports
Limited-time offer
Uploaded on 03/21/2021
11 documents
1 / 52
This page cannot be seen from the preview
Don't miss anything!
On special offer
printBoard(); System.out.println("X's will play first. Enter a slot number to place X in:"); while (winner == null) { int numInput; try { numInput = in.nextInt(); if (!(numInput > 0 && numInput <= 9)) { System.out.println("Invalid input; re-enter slot number:"); continue; } } catch (InputMismatchException e) { System.out.println("Invalid input; re-enter slot number:"); continue; } if (board[numInput-1].equals(String.valueOf(numInput))) { board[numInput-1] = turn; if (turn.equals("X")) { turn = "O"; } else { turn = "X"; } printBoard(); winner = checkWinner(); } else { System.out.println("Slot already taken; re-enter slot number:"); continue; } } if (winner.equalsIgnoreCase("draw")) { System.out.println("It's a draw! Thanks for playing."); } else { System.out.println("Congratulations! " + winner + "'s have won! Thanks for playing."); } } static String checkWinner() { for (int a = 0; a < 8; a++) { String line = null; switch (a) { case 0: line = board[0] + board[1] + board[2]; break; case 1: line = board[3] + board[4] + board[5]; break; case 2: line = board[6] + board[7] + board[8];
import java.util.Scanner; import static java.lang.System.*; import java.util.HashSet; import java.util.LinkedList; import java.util.Queue; import java.util.Set; public class UsingBFS { public static int MAX_CAPACITY_OF_LEFT_JUG, MAX_CAPACITY_OF_RIGHT_JUG, MAX_DEPTH_OF_TREE; public static Queue
Node newNode4 = new Node(MAX_CAPACITY_OF_LEFT_JUG, node.y- MAX_CAPACITY_OF_LEFT_JUG+node.x); if(checkIfEncountered(newNode4)) ++tempVariableForChildren; } else if(node.x != 0){ // => y= if(node.x>MAX_CAPACITY_OF_RIGHT_JUG){ Node newNode = new Node(node.x- MAX_CAPACITY_OF_RIGHT_JUG, MAX_CAPACITY_OF_RIGHT_JUG); if(checkIfEncountered(newNode)) ++tempVariableForChildren; } else{ Node newNode = new Node(0, node.x); if(checkIfEncountered(newNode)) ++tempVariableForChildren; Node newNode2 = new Node(MAX_CAPACITY_OF_LEFT_JUG, node.x); if(checkIfEncountered(newNode2)) ++tempVariableForChildren; Node newNode3 = new Node(node.x, MAX_CAPACITY_OF_RIGHT_JUG); if(checkIfEncountered(newNode3)) ++tempVariableForChildren; } } else if(node.y != 0){ // => x= Node newNode = new Node(node.y, 0); if(checkIfEncountered(newNode)) ++tempVariableForChildren; Node newNode2 = new Node(node.y, MAX_CAPACITY_OF_RIGHT_JUG); if(checkIfEncountered(newNode2)) ++tempVariableForChildren; Node newNode3 = new Node(MAX_CAPACITY_OF_LEFT_JUG, node.y); if(checkIfEncountered(newNode3)) ++tempVariableForChildren; } else { // When x and y are both 0 Node newNode1 = new Node(MAX_CAPACITY_OF_LEFT_JUG, 0); if(checkIfEncountered(newNode1)) ++tempVariableForChildren; Node newNode2 = new Node(0, MAX_CAPACITY_OF_RIGHT_JUG); if(checkIfEncountered(newNode2)) ++tempVariableForChildren; }
//queue.add(new Node(-1, -1)); // noOfChildrenOfNodes.add(tempVariableForChildren); if(!flag) queue.add(new Node(-1, -1)); flag = true; } } public static boolean checkIfEncountered(Node node){ if(!alreadyEncountered.contains(node)){ queue.add(node); alreadyEncountered.add(node); return true; //Child added to the queue } return false; //No new child added to the queue } }
//if the state size is equal to n we have a valid configuration if(this.n == state.size()){ return state; } //branch computations for(int i = 0; i < this.n; i++){ //push i onto the stack state.add(i); //check if placement onto stack is valid according to game rules if(isValid(state)){ //if the placement is valid, we need to try to place something in the next column LinkedList
s.append("|"); } return s.toString(); } public void solve(){ this.state = compute(new LinkedList
Class Node { Char data; Public Node(char c) { this.data=c; } }
Chromatic Number: The smallest number of colors needed to color a graph G is called its chromatic number. Source Code: import java.io.FileNotFoundException; public class MapColoringProblem { Graph graph; int[] color_config_array;//this array holds the color number for each vertex corresponding to the index //For example - if the number of colors is 3. Then then different colours will be repreented by numbers 1,2 and 3. //this method prints which vertex will be of which color public void printConfiguration() { for(int i=0;i<color_config_array.length;i++) { System.out.println("The " +(i+1)+"(th) vertex will be colored in color number "+color_config_array[i]); } } //this method colors the graph using recursive backtracking in all possible combinations and returns true of the graph can //be colored in the given number of colors public boolean colorGraph(int vertex_num,int number_of_colours) { if(vertex_num==graph.adjLists.length)//base condition { return checkGraph(); } for(int i=1;i<=number_of_colours;i++) { color_config_array[vertex_num]=i; if(colorGraph(vertex_num+1,number_of_colours)==true) { return true; } } return false; } //this method returns true of graph is colored properly and false otherwise public boolean checkGraph() { boolean result=true; int no_Of_vertex=graph.adjLists.length;
Source Code: public class MapColoringProblem { Graph graph; int[] color_config_array;//this array holds the color number for each vertex corresponding to the index //For example - if the number of colors is 3. Then then different colours will be repreented by numbers 1,2 and 3. //this method prints which vertex will be of which color public void printConfiguration() { for(int i=0;i<color_config_array.length;i++) { System.out.println("The " +(i+1)+"(th) vertex will be colored in color number "+color_config_array[i]); } } //this method colors the graph using recursive backtracking in all possible combinations and returns true of the graph can //be colored in the given number of colors public boolean colorGraph(int vertex_num,int number_of_colours) { if(vertex_num==graph.adjLists.length)//base condition { return checkGraph(); } for(int i=1;i<=number_of_colours;i++) { color_config_array[vertex_num]=i; if(colorGraph(vertex_num+1,number_of_colours)==true) { return true; } } return false; } //this method returns true of graph is colored properly and false otherwise public boolean checkGraph() { boolean result=true; int no_Of_vertex=graph.adjLists.length; for(int i=0;i<no_Of_vertex;i++) { int color_of_vertex=color_config_array[i];