You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+81Lines changed: 81 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -291,6 +291,87 @@ class BinaryTree
291
291
}
292
292
}
293
293
294
+
### Graph Data Structure And Algorithms :
295
+
296
+
A Graph is a non-linear data structure consisting of nodes and edges. The nodes are sometimes also referred to as vertices and the edges are lines or arcs that connect any two nodes in the graph.
297
+
298
+
#### Find the minimum cost to reach destination using a train
299
+
300
+
There are N stations on route of a train. The train goes from station 0 to N-1. The ticket cost for all pair of stations (i, j) is given where j is greater than i. Find the minimum cost to reach the destination.
301
+
302
+
Consider the following example:
303
+
304
+
Input:
305
+
cost[N][N] = { {0, 15, 80, 90},
306
+
{INF, 0, 40, 50},
307
+
{INF, INF, 0, 70},
308
+
{INF, INF, INF, 0}
309
+
};
310
+
There are 4 stations and cost[i][j] indicates cost to reach j
311
+
from i. The entries where j < i are meaningless.
312
+
313
+
Output:
314
+
The minimum cost is 65
315
+
The minimum cost can be obtained by first going to station 1
316
+
from 0. Then from station 1 to station 3.
317
+
318
+
The minimum cost to reach N-1 from 0 can be recursively written as following:
319
+
320
+
minCost(0, N-1) = MIN { cost[0][n-1],
321
+
cost[0][1] + minCost(1, N-1),
322
+
minCost(0, 2) + minCost(2, N-1),
323
+
........,
324
+
minCost(0, N-2) + cost[N-2][n-1] }
325
+
326
+
class shortest_path
327
+
{
328
+
329
+
static int INF = Integer.MAX_VALUE,N = 4;
330
+
// A recursive function to find the shortest path from
331
+
// source 's' to destination 'd'.
332
+
static int minCostRec(int cost[][], int s, int d)
333
+
{
334
+
// If source is same as destination
335
+
// or destination is next to source
336
+
if (s == d || s+1 == d)
337
+
return cost[s][d];
338
+
339
+
// Initialize min cost as direct ticket from
340
+
// source 's' to destination 'd'.
341
+
int min = cost[s][d];
342
+
343
+
// Try every intermediate vertex to find minimum
344
+
for (int i = s+1; i<d; i++)
345
+
{
346
+
int c = minCostRec(cost, s, i) +
347
+
minCostRec(cost, i, d);
348
+
if (c < min)
349
+
min = c;
350
+
}
351
+
return min;
352
+
}
353
+
354
+
// This function returns the smallest possible cost to
355
+
// reach station N-1 from station 0. This function mainly
356
+
// uses minCostRec().
357
+
static int minCost(int cost[][])
358
+
{
359
+
return minCostRec(cost, 0, N-1);
360
+
}
361
+
362
+
public static void main(String args[])
363
+
{
364
+
int cost[][] = { {0, 15, 80, 90},
365
+
{INF, 0, 40, 50},
366
+
{INF, INF, 0, 70},
367
+
{INF, INF, INF, 0}
368
+
};
369
+
System.out.println("The Minimum cost to reach station "+ N+
370
+
" is "+minCost(cost));
371
+
}
372
+
373
+
}
374
+
294
375
### Bubble sort :
295
376
296
377
Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in wrong order.
0 commit comments