Wednesday, 9 January 2019

Java program to implement local search algorithm (Hill climbing algorithm) (AI)


import java.util.Scanner;

public class hillClimbing {

public static void main(String[] args) {
int n,i,j;
Scanner sc=new Scanner(System.in);
System.out.println("Enter number of nodes in graph");
n=sc.nextInt();
int[][] graph=new int[n][n];
for(i=0;i<n;i++)
for(j=0;j<n;j++)
graph[i][j]=0;
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
System.out.println("Is "+i+" is connected to "+ j);
graph[i][j]=sc.nextInt();
}
}
System.out.println("The adjacency matrix is:");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{ System.out.print(graph[i][j]+ "\t");
}
System.out.println();
}
}
}

Output:

Enter number of nodes in graph
4
Is 0 is connected to 1
0
Is 0 is connected to 2
1
Is 0 is connected to 3
0
Is 1 is connected to 2
1
Is 1 is connected to 3
1
Is 2 is connected to 3
0
The adjacency matrix is:
0 0 1 0
0 0 1 1
0 0 0 0
0 0 0 0


No comments:

Post a Comment