Question

I need a java code to choose the shortest path through nodes using hill climbing algorithm.

I need a java code to choose the shortest path through nodes using hill climbing algorithm.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

package function;
import java.util.Scanner;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Stack;

class Ext
{
String source;
String destination;
int distance;
boolean skip;
Ext(String f, String t, int d)
{
    source = f;
    destination = t;
    distance = d;
    skip = false;
}
}

public class Extra
{
final int MAX = 100;
Ext flights[] = new Ext[MAX];
int numFlights = 0;
Stack btStack = new Stack();
public static void main(String args[])
{
    String destination, source;
    Extra ob = new Extra();
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    ob.setup();
    try
    {
      System.out.print("Source? ");
      source = br.readLine();
      System.out.print("Destination? ");
      destination = br.readLine();
      ob.isflight(source, destination);
      if (ob.btStack.size() != 0)
        ob.route(destination);
    }
    catch (IOException exc)
    {
      System.out.println("Error on input.");
    }
}

// Initialize the flight database.
void setup()
{
    addFlight("New York", "Chicago", 900);
    addFlight("Chicago", "Denver", 1000);
    addFlight("New York", "Torondestination", 500);
    addFlight("New York", "Denver", 1800);
    addFlight("Torondestination", "Calgary", 1700);
    addFlight("Torondestination", "Los Angeles", 2500);
    addFlight("Torondestination", "Chicago", 500);
    addFlight("Denver", "Urbana", 1000);
    addFlight("Denver", "Housdestinationn", 1000);
    addFlight("Housdestinationn", "Los Angeles", 1500);
    addFlight("Denver", "Los Angeles", 1000);
}

void addFlight(String source, String destination, int dist)
{
    if (numFlights < MAX)
    {
      flights[numFlights] = new Ext(source, destination, dist);
      numFlights++;
    }
    else
      System.out.println("Flight database full.\n");
}

void route(String destination)
{
    Stack rev = new Stack();
    int dist = 0;
    Ext f;
    int num = btStack.size();
    for (int i = 0; i < num; i++)
      rev.push(btStack.pop());
    for (int i = 0; i < num; i++)
    {
      f = (Ext) rev.pop();
      System.out.print(f.source + " destination ");
      dist += f.distance;
    }

    System.out.println(destination);
    System.out.println("Distance is " + dist);
}

int match(String source, String destination)
{
    for (int i = numFlights - 1; i > -1; i--)
    {
      if (flights[i].source.equals(source) && flights[i].destination.equals(destination)&& !flights[i].skip)
      {
        flights[i].skip = true;
        return flights[i].distance;
      }
    }
    return 0;
}

Ext find(String source)
{
    int pos = -1;
    int dist = 0;
    for (int i = 0; i < numFlights; i++)
    {
      if (flights[i].source.equals(source) && !flights[i].skip)
      {
        if (flights[i].distance > dist)
        {
          pos = i;
          dist = flights[i].distance;
        }
      }
    }

    if (pos != -1)
    {
      flights[pos].skip = true;
      Ext f = new Ext(flights[pos].source, flights[pos].destination,flights[pos].distance);
      return f;
    }

    return null;
}

void isflight(String source, String destination)
{
    int dist;
    Ext f = null;
    dist = match(source, destination);
    if (dist != 0)
    {
      btStack.push(new Ext(source, destination, dist));
      return;
    }
    if (f != null)
    {
      btStack.push(new Ext(source, destination, f.distance));
      isflight(f.destination, destination);
    }
    else if (btStack.size() > 0)
    {
      f = (Ext) btStack.pop();
      isflight(f.source, f.destination);
    }
}
}

Add a comment
Know the answer?
Add Answer to:
I need a java code to choose the shortest path through nodes using hill climbing algorithm.
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT