Question: I need Help implementing Greedy search for a car that looks for active open parking spots. Below is code I wrote for uniform cost search
I need Help implementing Greedy search for a car that looks for active open parking spots. Below is code I wrote for uniform cost search but instead of Uni search I would like to have greedy implemented instead. The output of the code is below.
If you need any more information please comment below I'll respond fast
I can also send the full java files through email with correct indentation

package practice1; import java.util.Random;
public class Uniformcostsearch { public static Object[][] visualRep; // Used to display all the information in a table (2D array) //public static ListNode[]openSpots = new ListNode[8]; public static void parkingDisplay(ListNode startState, int stage) { if(stage == 0) // Label the different displays. { System.out.println("Starting Parking Spot display "); // Given parking spots. }else if(stage == 1) { System.out.println("Parking Spot display: Given spaces "); // Parking spots available right now. }else if(stage == 2) { System.out.println("[Parking Spot display: Achieved Optimal Parking spot] "); // Found the least costing spot. } visualRep = new Object[4][7]; // Declared array spaces. int row = 0, col = 0; ListNode temp = new ListNode(); temp = startState.next; ListNode temp2 = new ListNode(); temp2 = startState.prev; ListNode temp3 = new ListNode(); temp3 = startState.next; ListNode temp4 = new ListNode(); temp4 = startState.prev; for(row = 0; row
// Start state. ListNode start = new ListNode(0); // Link both lines of parking to the start state. start.next = pSpots.head; start.prev = pSpots2.head; // Given Distances and Parking statuses. parkingDisplay(start, 0); // Table with given distances and default statuses. parkingDisplay(start, 1); // Table with given distances and given statuses. // Uniform Search Cost Algorithm call System.out.println(); Uni(start); // Table with given distances and given statuses. This table will display the parking spot that was optimally picked. parkingDisplay(start, 2); // Best space }
public static void Uni(ListNode startState) { //int checkOrder = 0; // leastCost for both lines of parking. int leastCost = 0; int leastCost2 = 0; ListNode temp = new ListNode(); ListNode temp2 = new ListNode(); temp = startState.next; temp2 = startState.prev; // Added the least costs at the beginning to initial compare the first two spots. leastCost = temp.distance; leastCost2 = temp2.distance; while(temp != null && temp2 != null) // Keeps going until there isn't a spot open unless the best spot is found. { // Checked which side of parking has the leastCost so far and if the node with the least cost is currently open. if(leastCost
}
***********************************************************************************************************************************
package practice1;
public class LinkedList { public ListNode head; public LinkedList () { head = null; } /* * Implement the LIST-SEARCH(L, k) function */ public ListNode search (int k) { ListNode newNode = new ListNode(); newNode = this.head; while(newNode != null && newNode.key != k) { newNode = newNode.next; } return newNode; } /* * Implement the LIST-INSERT(L, x) function * Note that x is a integer value, not a ListNode */ public void insert (int x) { ListNode newNode = new ListNode(x); newNode.next = this.head; if(this.head != null) { this.head.prev = newNode; } newNode.prev = null; this.head = newNode; } /* * Implement the LIST-DELETE(L, x) function */ public void delete (ListNode x) { if(x.prev != null) { x.prev.next = x.next; }else { this.head = x.next; } if(x.next != null) { x.next.prev = x.prev; } } /* * Convert a LinkedList to a string in the format of [#elements] */ public String toString () { String str; ListNode n; str = "["; n = this.head; while (n != null) { str += n.key + ","; n = n.next; } str += "]"; return str; } /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub LinkedList l; l = new LinkedList(); for (int i = 0; i
} package practice1;
public class LinkedList { public ListNode head; public LinkedList () { head = null; } /* * Implement the LIST-SEARCH(L, k) function */ public ListNode search (int k) { ListNode newNode = new ListNode(); newNode = this.head; while(newNode != null && newNode.key != k) { newNode = newNode.next; } return newNode; } /* * Implement the LIST-INSERT(L, x) function * Note that x is a integer value, not a ListNode */ public void insert (int x) { ListNode newNode = new ListNode(x); newNode.next = this.head; if(this.head != null) { this.head.prev = newNode; } newNode.prev = null; this.head = newNode; } /* * Implement the LIST-DELETE(L, x) function */ public void delete (ListNode x) { if(x.prev != null) { x.prev.next = x.next; }else { this.head = x.next; } if(x.next != null) { x.next.prev = x.prev; } } /* * Convert a LinkedList to a string in the format of [#elements] */ public String toString () { String str; ListNode n; str = "["; n = this.head; while (n != null) { str += n.key + ","; n = n.next; } str += "]"; return str; } /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub LinkedList l; l = new LinkedList(); for (int i = 0; i
}
Starting Parking Spot display Distance 8 7 00 7 1 9 Status Open Open Open Open Open Open Distance 3 2 7 8 5 7 Status Open Open Open Open Open Open Parking Spot display: Given spaces Distance 8 7 8 7 1 9 Status Open Parked Parked Parked Open Parked Distance 3 2 7 8 5 7 Status Parked Parked Open Open Open Parked package practicel; public class ListNode { public int key; public ListNode prev; public ListNode next; // Added attribute(s) for search method assignment AI public int distance; // Distance from start state. public String parked = "Open"; public ListNode () { prev = next = null; } public ListNode (int _key) { key = _key; distance = key; prev = next = null; } } Starting Parking Spot display Distance 8 7 00 7 1 9 Status Open Open Open Open Open Open Distance 3 2 7 8 5 7 Status Open Open Open Open Open Open Parking Spot display: Given spaces Distance 8 7 8 7 1 9 Status Open Parked Parked Parked Open Parked Distance 3 2 7 8 5 7 Status Parked Parked Open Open Open Parked package practicel; public class ListNode { public int key; public ListNode prev; public ListNode next; // Added attribute(s) for search method assignment AI public int distance; // Distance from start state. public String parked = "Open"; public ListNode () { prev = next = null; } public ListNode (int _key) { key = _key; distance = key; prev = next = null; } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
