Count occurrence of integers in an array - java

I have an integer array: int[] numbers = new int[...n]; // n being limitless.
Where all the numbers are between 0 and 100.
Say numbers[] was equal to: [52, 67, 32, 43, 32, 21, 12, 5, 0, 3, 2, 0, 0];
I want to count how often each of those numbers occur.
I've got a second array: int[] occurrences = new int[100];.
I'd like to be able to store the amounts like such:
for(int i = 0; i < numbers.length; i++) {
// Store amount of 0's in numbers[] to occurrences[0]
// Store amount of 1's in numbers[] to occurrences[1]
}
So that occurrences[0] would be equal to 3, occurrences[1] would be equal to 0 etc.
Is there any efficient way of doing this without having to resort to external libraries? thanks.

You can simply do something like this:
for (int a : numbers) {
occurrences[a]++;
}
Also, if you mean 0 to 100 inclusive then occurrences will need to be of size 101 (i.e. 100 will need to be the maximum index).
You might also want to perform an "assertion" to ensure that each element of numbers is indeed in the valid range before you update occurrences.

Updated to put results in the 100-array.
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
/**
<P>{#code java IntOccurancesInArray}</P>
**/
public class IntOccurancesInArray {
public static final void main(String[] igno_red) {
int[] ai = new int[]{52, 67, 32, 43, 32, 21, 12, 5, 0, 3, 2, 0, 0};
Map<Integer,Integer> mpNumWHits = new TreeMap<Integer,Integer>();
for(int i = 0; i < ai.length; i++) {
int iValue = ai[i];
if(!mpNumWHits.containsKey(iValue)) {
mpNumWHits.put(iValue, 1);
} else {
mpNumWHits.put(iValue, (mpNumWHits.get(iValue) + 1));
}
}
Set<Integer> stInts = mpNumWHits.keySet();
Iterator<Integer> itrInts = stInts.iterator();
int[] ai100 = new int[100];
int i = 0;
while(itrInts.hasNext()) {
int iValue = itrInts.next();
int iHits = mpNumWHits.get(iValue);
System.out.println(iValue + " found " + iHits + " times");
ai100[iValue] = iHits;
}
for(int j = 0; j < ai100.length; j++) {
if(ai100[j] > 0) {
System.out.println("ai100[" + j + "]=" + ai100[j]);
}
}
}
}
Output:
[C:\java_code\]java IntOccurancesInArray
0 found 3 times
2 found 1 times
3 found 1 times
5 found 1 times
12 found 1 times
21 found 1 times
32 found 2 times
43 found 1 times
52 found 1 times
67 found 1 times
ai100[0]=3
ai100[2]=1
ai100[3]=1
ai100[5]=1
ai100[12]=1
ai100[21]=1
ai100[32]=2
ai100[43]=1
ai100[52]=1
ai100[67]=1

This method is useful for knowing occurrences of all elements
You can reduce the space by finding the length of new array using sorting and taking value of last element + 1
import java.util.Arrays;
public class ArrayMain {
public static void main(String[] args) {
int a[] = {52, 67, 32, 43, 32, 21, 12, 5, 0, 3, 2, 0, 0};
Arrays.sort(a);
int len=a[a.length-1]+1;
int count[]=new int[len];
for(int n:a){
count[n]++;
}
for(int j=0;j<count.length;j++){
if(count[j]>=1){
System.out.println("count:"+j+"---"+count[j]);
}
}
}
}
Time Complexity : O(n)
Space Complexity : O(R) // last element value +1
Note : Creating new array may not be good idea if you have extreme numbers like 1, 2 and 96, 99 etc in terms of space.
For this case sorting and comparing next element is better approach

import java.util.Scanner;
public class CountNumOccurences {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
int[] frequency = new int[100];
System.out.println("Enter the first integer: ");
int number = input.nextInt();
//Enter up to 100 integers, 0 to terminate
while (number != 0){
++frequency[number];
//read the next integer
System.out.print(
"Enter the next int value (zero to exit): ");
number = input.nextInt();
}
input.close();
System.out.println("Value\tFrequency");
for (int i = 0; i < frequency.length; i++) {
if (frequency[i] > 0){
if (frequency[i] > 1)
System.out.println(i + " occurs " + frequency[i] + " times");
else
System.out.println(i + " occurs " + frequency[i] + " time");
}
}
}
}

Related

Maximum difference between final and initial sum

I am new to programming and having trouble solving one task. I have several input example. First line contains two numbers m (number of digits on paper, 1<m<1000) and h (limit on the number of operations, 1<h<1000). I have the opportunity, no more than h times, to take any number from a piece of paper (means m), then paint over one of the old digits, and write a new arbitrary digit in its place. By what maximum value can I be able to increase the sum of all the numbers on the piece of paper?
First example:
Input:
5 2 //m and h
1 3 1 4 5 //m = 5, so I can add 5 arbitrary numbers and h=2, so I can change 2 numbers
Output:
16 // cause I changed 1 and 1 to 9 and 9, so the difference 8 and 8 and the sum is 16
Second example:
Input:
3 1
99 5 85
Output:
10 //85 to 95, so the difference is 10
Third example:
Input:
1 10
9999
Output:
0 // nothing to be change
What I have for now:
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number: ");
int m = sc.nextInt();
int h = sc.nextInt();
System.out.println("Entered: " + m);
System.out.println("Entered: " + h);
int[] numbers = new int[m];
for(int i = 0; i < m; ++i) {
numbers[i] = sc.nextInt();
}
Arrays.sort(numbers);
//here is my logic: I am changing 1 to 9
for (int i = 0; i < h; i++) {
if (numbers[i] < 10) {
numbers[i] = 9;
}
else if (numbers[i] > 9 and numbers[i] < 100) {
numbers[i] = 99;
}
}
sc.close();
My logic can work for the first example, but for the second example it won't work. Can you assist me if I am using right logic or is there any easier way to solve this? Thanks in advance.
I quickly came up with below solution
public static void calculateMax(int m,int h, int[] arr){
int sum = 0;
ArrayList<Integer> al = new ArrayList<>();
for(int i=0;i<arr.length;i++){
String stringNum = Integer.toString(arr[i]);
int num = Integer.parseInt(stringNum.substring(0, 1));
if(num!=9){
al.add(Integer.parseInt(("9"+stringNum.substring(1)))-arr[i]);
continue;
}
al.add(0);
}
Collections.sort(al);
int j = al.size()-1;
for(int i=0;i<h && j>0;i++){
sum+=al.get(j--);
}
System.out.println(sum);
}
Here what I am doing is basically, calculating for each number what we can get as maximum by removing one digit and replacing by 9. And I am storing them in a list. Then we can sort that list and get 'h' largest numbers from stored list, and essentially getting the sum of them and printing it.
Break each input number into its digits times the appropriate power of 10. Sort these by powers of 10 descending, digits ascending. Apply your operation in this order.
E.g., 876, 12, 42 -> 800, 70, 6, 10, 2, 40, 2 -> 800, 10, 40, 70, 2, 2, 6.

Relative frequency in java

I want to create a program to help me with the statistics, but I'm having problems from the beginning and I'm making a huge mess to calculate the relative frequency of an array with random numbers and only one dimension.
For example to generate these numbers:
{3, 5, 5, 2, 4, 1, 3, 5, 4}
I want that the program tell me that the 3 is repeated 2 times, the 4 3 times and 5 5 times
I've created a class to sort these values ​​in order to calculate the median, the first and third quartile, but I still do not know how to find the frequency in order to calculate other values
Thanks for your time
PS: Do not know if this affects anything but I'm using netbeans
You are looking for this for sure: Collections: frequency
If you dont have a Collection, convert your array to list first:
Collections.frequency(Arrays.asList(yourArray), new Integer(3))
If your range of numbers is relatively small, using an array of counters could be preferred.
For example, if your random numbers are in the interval [1,5] then you can use an array of size 5 to store and update the frequency counters:
int[] numbers = {3, 5, 5, 2, 4, 1, 3, 5, 4} ;
int[] frequencies = new int[5];
for(int n : numbers)
frequencies[n-1]++;
Output array (frequencies):
1 1 2 2 3
EDIT:
This method can be applied to all ranges. For example, let's say you have numbers in the range [500,505]:
int[] frequencies = new int[6];
for(int n : numbers)
frequencies[n-500]++;
Edit: You can use a map for storing frequency like below :
import java.util.HashMap;
import java.util.Map;
public class Frequency {
public static void main(String[] args) {
int[] nums = { 3, 5, 5, 2, 4, 1, 3, 5, 4 };
int count = 1;
// number,frequency type map.
Map<Integer, Integer> frequencyMap = new HashMap<Integer, Integer>();
for (int i = 0; i < nums.length; i++) {
if (nums[i] != -1) {
for (int j = i + 1; j < nums.length; j++) {
if (nums[j] != -1) {
if (nums[i] == nums[j]) {
// -1 is an indicator that this number is already counted.
// You should replace it such a number which is sure to be not coming in array.
nums[j] = -1;
count++;
}
}
}
frequencyMap.put(nums[i], count);
count = 1;
}
}
for (Map.Entry<Integer, Integer> entry : frequencyMap.entrySet()) {
System.out.println(" Number :" + entry.getKey()
+ " has frequence :" + entry.getValue());
}
}
}
With Output :
Number :1 has frequence :1
Number :2 has frequence :1
Number :3 has frequence :2
Number :4 has frequence :2
Number :5 has frequence :3
int[] numbers = {100, 101, 102, 103, 5 , 4, 4 , 6} ;
Map<Integer, Integer> m = new HashMap<Integer, Integer>();
for(int num: numbers){
if(m.containsKey(num)){
m.put(num, m.get(num)+1);
}else{
m.put(num, 1);
}
}
for (Map.Entry<Integer, Integer> entry : m.entrySet()) {
System.out.println("Key: " + entry.getKey() + " | Frequencey: " + entry.getValue());
}

How to get lowest 3 elements in an int array

Hi I want to get the lowest 3 elements of an array. By lowest I mean the minimum value. I cannot use the collections.Sort method as I need to know the index of the elements. Therefore I am using the following code to get the lowest, but I need to know how I can get the lowest 3.
int minimum = grades[1];
int index = 1;
for(i=1; i<= numberOfStudents; i++){
if (grades[i]<minimum){
minimum = grades[i];
index = i;
}
}
Here is a really simple way of doing it:
public static void main(String[] args) {
int[] myArray = { 5, 8, 12, 9, 50, 11, 4 };
System.out.println(Arrays.toString(myArray));
System.out.println(Arrays.toString(getThreeLowest(myArray)));
}
private static int[] getThreeLowest(int[] array) {
int[] lowestValues = new int[3];
Arrays.fill(lowestValues, Integer.MAX_VALUE);
for(int n : array) {
if(n < lowestValues[2]) {
lowestValues[2] = n;
Arrays.sort(lowestValues);
}
}
return lowestValues;
}
This outputs:
[5, 8, 12, 9, 50, 11, 4]
[4, 5, 8]
The call to Arrays.sort is only done to the local array, not your main array. The reason it does this is just to simplify the comparison against n.
Building off what you had
int[] grades = { 100, 99, 98, 97, 10, 95, 11, 9, 94 };
int numberOfStudents = grades.length;
int minimum = grades[1];
int minimum2 = grades[1];
int minimum3 = grades[1];
int index = 1;
int index2 = 1;
int index3 = 1;
for(int i=1; i< numberOfStudents; i++){
if (grades[i]<minimum3 && grades[i]>=minimum2){
minimum3 = grades[i];
index3 = i;
}
if (grades[i]<minimum2 && grades[i]>=minimum){
//We have a new 2nd lowest - shift previous 2nd lowest up
minimum3 = minimum2;
index3 = index2;
minimum2 = grades[i];
index2 = i;
}
if (grades[i]<minimum){
//We have a new lowest - shift previous lowest up
minimum3 = minimum2;
index3 = index2;
minimum2 = minimum;
index2 = index;
minimum = grades[i];
index = i;
}
}
System.out.println("Smallest is at " + index + " with value of " + minimum);
System.out.println("Next Smallest is at " + index2 + " with value of " + minimum2);
System.out.println("Next Smallest is at " + index3 + " with value of " + minimum3);
This may be a bit 'too much' but off the top of my head possible that you could make an array of objects, each object containing the value and index it has in the original 'grades' array and sort that?
The only other way I can think of is to good through the array and manually keep track of the 3 lowest elements and their indexes like what you're already doing...
Take three variables: the smallest, second smallest and third smallest.
In the same way you are finding your smallest element, find at each step which are the three smallest elements.
You need to check if any element is smaller than the smallest number, or it is between the smallest and the second smallest, or it is between the second smallest and the third smallest.
As this is probably an assignment, task or homework, i will not write the code here.
Can we do
int[] myArray = { 5, 8, 12, 9, 50, 11, 4 };
Arrays.sort(myArray);
System.out.println(myArray[0] +","+ myArray[1] +","+ myArray[2]);

Java Interview Task: Complete the Java method for a sorted array [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Find a pair of elements from an array whose sum equals a given number
I was recently presented with the following Java interview question.
The target is to complete the method task with only a single pass over the input array.
I claimed it is not possible to complete this task on a single pass over the array but I was met with the usual silence, pause and then the interviewer proclaimed the interview was at an end without giving me the answer.
public class SortedArrayOps {
public SortedArrayOps() {
}
// Print at the system out the first two ints found in the sorted array: sortedInts[] whose sum is equal to Sum in a single pass over the array sortedInts[] with no 0 value allowed.
// i.e. sortedInts[i] + sortedInts[?] = Sum where ? is the target index to be found to complete the task.
static void PrintIntSumValues(int Sum, int sortedInts[]) {
// need to test to see if the Sum value is contained in the array sortedInts. And, if not do nothing.
for(int i=0; i<sortedInts.length; i++) {
// ... do some work: algebra and logic ...
// System.out.println sortedInts[i]+sortedInts[?] sums to Sum.
}
}
public static void main(String[] args) {
final int[] sortedArray = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50};
PrintIntSumValues(48, sortedArray);
}
}
I am not sure which values in the array you are looking for (what does "first two" ints mean? Minimum sum of their indices? One is smallest? Any two that happen to pop out first?), but this solution is O(n), takes one pass, uses no additional data structures, and only uses one extra int. It does not always find the two indices closest together, nor does it always find the "first", whatever that might mean. I believe that it will always find the two whose sum is smallest (until you guys find a counter example).
Let me know if you guys find any bugs with it:
class Test {
public static void main(String[] args) {
int[] sortedArray = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
PrintIntSumValues(6, sortedArray);
sortedArray = new int[] {1, 2,3, 12, 23423};
PrintIntSumValues(15, sortedArray);
sortedArray = new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
PrintIntSumValues(100, sortedArray);
sortedArray = new int[] {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50};
PrintIntSumValues(48, sortedArray);
}
// Print at the system out the first two ints found in the sorted array: sortedInts[] whose sum is equal to Sum in a single pass over the array sortedInts[] with no 0 value allowed.
// i.e. sortedInts[i] + sortedInts[?] = Sum where ? is the target index to be found to complete the task.
static void PrintIntSumValues(int Sum, int sortedInts[]) {
// need to test to see if the Sum value is contained in the array sortedInts. And, if not do nothing.
int offset = sortedInts.length-1;
for(int i=0; i<sortedInts.length; i++) {
// ... do some work: algebra and logic ...
if ((sortedInts[i] + sortedInts[offset]) == Sum){
System.out.println("sortedInts[" + i + "]+sortedInts[" + offset + "] sums to " + Sum + ".");
return;
} else {
int remaining = Sum - sortedInts[i];
if (remaining < sortedInts[i] ){
// We need something before i
if (remaining < sortedInts[offset]) {
// Even before offset
offset = 0 + (offset - 0)/2;
} else {
// Between offset and i
offset = offset + (i - offset)/2;
}
} else {
// We need something after i
if (remaining < sortedInts[offset]) {
// But before offset
offset = i + (offset - i)/2;
} else {
// Even after offset
offset = offset + (sortedInts.length - offset)/2;
}
}
}
}
System.out.println("There was no sum :(");
}
}
You can view the output here.
import java.util.HashMap;
public class SortedArrayOps {
public SortedArrayOps() {
}
// Print at the system out the first two ints found in the sorted array: sortedInts[] whose sum is equal to Sum in a single pass over the array sortedInts[] with no 0 value allowed.
// i.e. sortedInts[i] + sortedInts[?] = Sum where ? is the target index to be found to complete the task.
static void PrintIntSumValues(int Sum, int sortedInts[]) {
HashMap<Integer, Boolean> pool= new HashMap<Integer, Boolean> ();
for(int i=0; i<sortedInts.length; i++) {
int current = sortedInts[i];
int target = Sum - current;
if (pool.containsKey(target)) {
System.out.println(String.format("%d and %d sum to %d", current, target, Sum));
break;
}
else {
pool.put(current, Boolean.TRUE);
}
}
}
public static void main(String[] args) {
final int[] sortedArray = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50};
PrintIntSumValues(48, sortedArray);
}
}
Here is a complete solution using a HashMap:
import java.util.HashMap;
public class Test
{
public static void main(String[] args)
{
final int[] sortedArray = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int sum = 6;
printSum(sum, sortedArray);
}
private static void printSum(int sum, int[] sortedArray)
{
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
for (int index = 0; index < sortedArray.length; index++)
{
int currentNumber = sortedArray[index];
int remainder = sum - currentNumber;
if (map.containsKey(remainder))
{
System.out.println(String.format("%d + %d = %d", currentNumber, remainder, sum));
break;
}
else
{
map.put(currentNumber, index);
}
}
}
}
This should work. You have two pointers, and make only a single pass through the data.
j = sortedInts.length - 1;
for(int i=0; i<sortedInts.length && j>=i; i++) {
sx = sortedInts[i];
while (sx + sortedInts[j] > Sum)
j++;
if (sx + sortedInts[j] == Sum)
...
}
Because the array of values is specific, the solution can be simplified as this,
public class SortedArrayOps {
public SortedArrayOps() {
}
// Print at the system out the first two ints found in the sorted array:
// sortedInts[] whose sum is equal to Sum in a single pass over the array
// sortedInts[] with no 0 value allowed.
// i.e. sortedInts[i] + sortedInts[?] = Sum where ? is the target index to
// be found to complete the task.
static void PrintIntSumValues(int Sum, int sortedInts[]) {
// need to test to see if the Sum value is contained in the array
// sortedInts. And, if not do nothing.
for (int i = 0; i < sortedInts.length; i++) {
// ... do some work: algebra and logic ...
// System.out.println sortedInts[i]+sortedInts[?] sums to Sum.
int remainder = Sum - sortedInts[i];
if( remainder <= sortedInts.length && remainder>0 && remainder!=sortedInts[i]) {
System.out.print(String.format("%d + %d = %d", sortedInts[i], sortedInts[remainder-1], Sum));
break;
}
}
}
public static void main(String[] args) {
final int[] sortedArray = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45,
46, 47, 48, 49, 50 };
PrintIntSumValues(48, sortedArray);
}
}

UVA#523 Minimum Transport Cost

UVA link: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=464
I'm lost as to what I'm doing now. I don't know why I'm producing incorrect values. I printed out the array after I looked for shortest path. And it produces this:
[0, 3, 8, 8, 4]
[3, 0, 5, 11, 7]
[8, 5, 0, 9, 12]
[8, 11, 9, 0, 4]
[4, 7, 12, 4, 0]
The sample input is:
1
0 3 22 -1 4
3 0 5 -1 -1
22 5 0 9 20
-1 -1 9 0 4
4 -1 20 4 0
5 17 8 3 1
1 3
3 5
2 4
With the sample input, I produce the output:
From 1 to 3 :
Path: 1-->2-->3
Total cost :8
From 3 to 5 :
Path: 3-->2-->5
Total cost :12
From 2 to 4 :
Path: 2-->5-->4
Total cost :11
If I look at my array, it seems to be correct. But the correct answer is:
From 1 to 3 :
Path: 1-->5-->4-->3
Total cost : 21
From 3 to 5 :
Path: 3-->4-->5
Total cost : 16
From 2 to 4 :
Path: 2-->1-->5-->4
Total cost : 17
I think I missed adding the tax, but I don't know where to add it. I tried adding tax[j] to the path[i][j] while doing the shortest path, and then subtracting the tax[x-1] and tax[y-1] (x-1 is my source, y-1 is my destination). It doesn't work, and messes up my path array (it gives value to loops; I don't really need that). I really don't know where to put the tax.
Here is my code for reference:
import java.util.*;
public class Main{
public static final int INF = 9999999;
public static int[][] path;
public static int[][] next;
public static int[] tax;
public static void main(String[] adsf){
Scanner pp = new Scanner(System.in);
int testCases = pp.nextInt();
boolean first = true;
pp.nextLine();
pp.nextLine();
while(testCases-- >0){
String[] s1 = pp.nextLine().split(" ");
int size = s1.length;
path = new int[size][size];
next = new int[size][size];
for(int i = 0; i < path.length; i++)
Arrays.fill(path[i],INF);
tax = new int[size];
for(int j = 0; j < path[0].length; j++){
path[0][j] = Integer.parseInt(s1[j]);
if(path[0][j]==-1)
path[0][j]=INF;
}
for(int i = 1; i < path.length;i++){
s1 = pp.nextLine().split(" ");
for(int j = 0; j < path[0].length; j++){
path[i][j] = Integer.parseInt(s1[j]);
if(path[i][j]==-1)
path[i][j]=INF;
}
}
for(int k=0; k<tax.length;k++){
tax[k] = pp.nextInt();
} pp.nextLine();
apsp();
int x,y;
//for(int i = 0; i < size; i++)
//System.out.println(Arrays.toString(path[i]));
while(pp.hasNextInt()){
if(!first)
System.out.println();
x=pp.nextInt();
y=pp.nextInt();
int cost = path[x-1][y-1];
System.out.println("From "+x+" to "+y+" :");
System.out.print("Path: ");
ArrayList<Integer> print = getpath(x-1,y-1);
for(int l = 0; l < print.size(); l++){
System.out.print(print.get(l)+1);
if(l!=print.size()-1) System.out.print("-->");
else System.out.println();
}
System.out.println("Total cost :"+cost);
first = false;
}
}
}
public static void apsp(){
for(int k=0;k<path.length;k++){
for(int i=0;i<path.length;i++){
for(int j=0;j<path.length;j++){
if (path[i][k] + path[k][j] + tax[j] < path[i][j] + tax[j]) {
path[i][j] = path[i][k]+path[k][j];
next[i][j] = k;
} else{
path[i][j] = path[i][j];
}
}
}
}
}
public static ArrayList<Integer> getpath (int i, int j) {
ArrayList<Integer> pat = getMidPath(i,j);
pat.add(0,i);
pat.add(j);
return pat;
}
public static ArrayList<Integer> getMidPath(int i, int j){
if(next[i][j]==0)
return new ArrayList<Integer>();
ArrayList<Integer> pat = new ArrayList<Integer>();
pat.addAll(getMidPath(i,next[i][j]));
pat.add(next[i][j]);
pat.addAll(getMidPath(next[i][j],j));
return pat;
}
}
The tax is applied:
whenever any cargo passing through one city, except for the source and the destination cities.
So you if you have a path:
a -> b -> c -> d
Then you should include the cost of the path (a,b) + tax(b) + (b,c) + tax(c) + (c,d).
For implementing that into your algorithm, you should be able to add the city taxes to the path lengths like this:
If you know you're starting at a and ending at d, then any directed path to a city x, where x isn't a or b, should be treated as the cost of x + the tax at city x.
You will need to revert the path cost values back to the original ones for the next path you want to calculate the value of and re-add the tax values in for the new starting point and destination.

Categories