I Have this code to get "shortest path" using Dijkstra
but I know a real problem involves sparse matrices (matrix populated primarily with zeros).
Is there a way to improve efficiency in this code?
If so what improvements?¡
The graph is:
using System;
namespace DijkstraAlgorithm
{
/*
* Dijkstra's algorithm, named after its discoverer, Dutch computer scientist Edsger Dijkstra,
* is a greedy algorithm that solves the single-source shortest path problem for a directed graph
* with non negative edge weights. For example, if the vertices (nodes) of the graph represent cities
* and edge weights represent driving distances between pairs of cities connected by a direct road,
* Dijkstra's algorithm can be used to find the shortest route between two cities. Also, this algorithm
* can be used for shortest path to destination in traffic network.
* Pseudocode
function Dijkstra(L[1..n, 1..n]) : array [2..n]
array D[2..n]
set C
C <- {2, 3, 4, 5, 6, …, n}
for i <- 2 to n
D[i] <- L[1,i]
repeat n - 2 times
v <- C // minimum D[v] extract to C
v <- C - {v}
for each w in C do
D[w] <- min(D[w], D[v] + L[v,w])
return D
*/
class Dijkstra
{
private int rank = 0;
private int[,] L;
private int[] C;
public int[] D;
private int trank = 0;
public Dijkstra(int paramRank,int [,]paramArray)
{
L = new int[paramRank, paramRank];
C = new int[paramRank];
D = new int[paramRank];
rank = paramRank;
for (int i = 0; i < rank; i++)
{
for (int j = 0; j < rank; j++) {
L[i, j] = paramArray[i, j];
}
}
for (int i = 0; i < rank; i++)
{
C[i] = i;
}
C[0] = -1;
for (int i = 1; i < rank; i++)
D[i] = L[0, i];
}
public void DijkstraSolving()
{
int minValue = Int32.MaxValue;
int minNode = 0;
for (int i = 0; i < rank; i++)
{
if (C[i] == -1)
continue;
if (D[i] > 0 && D[i] < minValue)
{
minValue = D[i];
minNode = i;
}
}
C[minNode] = -1;
for (int i = 0; i < rank; i++)
{
if (L[minNode, i] < 0)
continue;
if (D[i] < 0) {
D[i] = minValue + L[minNode, i];
continue;
}
if ((D[minNode] + L[minNode, i]) < D[i])
D[i] = minValue+ L[minNode, i];
}
}
public void Run()
{
for (trank = 1; trank <rank; trank++)
{
DijkstraSolving();
Console.WriteLine("iteration" + trank);
for (int i = 0; i < rank; i++)
Console.Write(D[i] + " ");
Console.WriteLine("");
for (int i = 0; i < rank; i++)
Console.Write(C[i] + " ");
Console.WriteLine("");
}
}
static void Main(string[] args)
{
int[,] L ={
{-1, 5, -1, -1, -1, 3, -1, -1},
{ 5, -1, 2, -1, -1, -1, 3, -1},
{-1, 2, -1, 6, -1, -1, -1, 10},
{-1, -1, 6, -1, 3, -1, -1, -1},
{-1, -1, -1, 3, -1, 8, -1, 5},
{ 3, -1, -1, -1, 8, -1, 7, -1},
{-1, 3, -1, -1, -1, 7, -1, 2},
{-1, -1, 10, -1, 5, -1, 2, -1}
};
Dijkstra clss = new Dijkstra((int)Math.Sqrt(L.Length),L);
clss.Run();
Console.WriteLine("Solution is");
foreach (int i in clss.D)
{
Console.WriteLine(i);
}
Console.WriteLine("Press Enter for exit.");
Console.Read();
}
}
}
In the code the matrix is an easy one, but if I have an sparse one...??
One possible improvement would be to stop using an array for the graph, and instead use a SparseMatrix class, which will result in better iteration over the matrix's entries.
You can probably find several libraries that provide SparseMatrix implementations:
http://www.extremeoptimization.com/QuickStart/SparseMatricesCS.aspx
http://sparsematrix.codeplex.com/
http://metanumerics.codeplex.com/
Another improvement might be to use multi-threads, but since each iteration of the Dijkstra algorithm is based on the previous iteration, that would have to be done extremely carefully.
Related
I can find a Java version for my question, but not C#. My current attempt goes crazy when attempting to add them. I feel like there is a simple fix, but I'm struggling to think of it.
// See https://aka.ms/new-console-template for more information
int[] input = { 28, 2, 3, -3, -2, 1, 2, 35, -1, 0, 0, -1 };
for (int i = 0; i < input.Length; i++)
{
int x = input[i];
int y = input[i++];
int output = x + y;
Console.WriteLine(output);
}
If I understand your task answer is (updated):
var sum = 0;
var input = new int[] { 28, 2, 3, -3, -2, 1, 2, 35, -1, 0, 0, -1 };
for (var i = 0; i < input.Length - 1; i++)
{
sum += input[i];
Console.WriteLine(sum);
}
This is the correct code :
int[] input = { 28, 2, 3, -3, -2, 1, 2, 35, -1, 0, 0, -1 };
for (int i = 0; i < input.Length - 1; i++)
{
int x = input[i];
int y = input[i + 1];
int output = x + y;
Console.WriteLine(output);
}
I'm trying to change values between two arrays, but im not getting get that right.
This is what I've done so far
I cant show the total of the first array and i cant make any swap at the second array
public static void exe4()
{
int[,] matriz1 = new int[,] { { -2, 3, 4, 11, -8 }, { -1, 0, -12, -6, 9 }, { 23, 4, 6, 8, -3 } };
int[,] matriz2 = new int[,] { { 2, 3, 8 }, { -2, -4, -5 }, { 0, 8, -14 }, { 3, 5, 6 }, { -9, -8, -1 } };
int troca1 = 0;
int troca2 = 0;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
troca1 = matriz1[i, j];
matriz1[1, 0] = matriz2[0, 2];
matriz1[1, 1] = matriz2[1, 2];
matriz1[1, 4] = matriz2[4, 1];
}
Console.WriteLine();
}
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
troca2 = matriz2[i, j];
matriz2[0, 2] = matriz1[1, 0];
matriz2[1, 2] = matriz1[1, 1];
matriz2[4, 2] = matriz1[1, 4];
}
Console.WriteLine();
}
Console.WriteLine("\nApós a troca dos valores:\n");
Console.WriteLine("Matriz1:\n");
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
Console.Write("{0}\t", matriz1[i, j]);
}
Console.WriteLine();
}
Console.WriteLine("\nMatriz2:\n");
Console.WriteLine();
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
Console.Write("{0}\t", matriz2[i, j]);
}
Console.WriteLine();
}
That's how it's supposed to be:
Analyzing your solution
It sounds like you're only interested in swapping very few and specific values in those matrices, so for-loops seem unnecessary as you already know the positions in the matrices you want to swap.
Apart from that I noticed that your for-loops only goes from 0 to 2 (for (int i = 0; i < 3; i++)) but your matrices have a length of 4 in one dimension, so the loops never reach the ends.
An alternative solution
If you just want to swap those specific values, you could do it like this:
int[,] matriz1 = new int[,]
{
{ -2, 3, 4, 11, -8 },
{ -1, 0, -12, -6, 9 },
{ 23, 4, 6, 8, -3 }
};
int[,] matriz2 = new int[,]
{
{ 2, 3, 8 },
{ -2, -4, -5 },
{ 0, 8, -14 },
{ 3, 5, 6 },
{ -9, -8, -1 }
};
int temporary;
// Swap A[1,0] with B[0,2]
temporary = matriz1[1,0];
matriz1[1,0] = matriz2[0,2];
matriz2[0,2] = temporary;
// Swap A[1,1] with B[1,2]
temporary = matriz1[1,0];
matriz1[1,1] = matriz2[1,2];
matriz2[1,2] = temporary;
// Swap A[1,4] with B[4,2]
temporary = matriz1[1,0];
matriz1[1,4] = matriz2[4,2];
matriz2[4,2] = temporary;
... or with a little helper function:
void Swap(int[,] matrixA, int row1, int col1, int[,] matrixB, int row2, int col2)
{
var temp = matrixA[row1, col1];
matrixA[row1, col1] = matrixB[row2, col2];
matrixB[row2, col2] = temp;
}
// ...
Swap(matriz1, 1, 0, matriz2, 0, 2);
Swap(matriz1, 1, 1, matriz2, 1, 2);
Swap(matriz1, 1, 4, matriz2, 4, 2);
Let's say I have an array with integers, which represent the daily changes in the price of a stock, as an example the following array:
[3, -1, -4, 1, 5, -9, 2, 6].
How would I find the amount of subarrays which have a sum between two values (lower and upper, so l <= s <= u), such as -1 (=lower) and 0 (=upper)? In this case, the answer would be 5. You can have the subarrays
[3, -1, -4, 1]
[-1]
[-1, -4, 1, 5, -9, 2, 6]
[1, 5, -9, 2]
[-9, 2, 6]
Another example array would be:
[4, 2, 2, -6, 7]
with lower bound 3, upper bound 4. The answer to this would be 3. I have tried the following very naïve approach, which I'm certain there are many faster alternatives for. I'm wondering how I can solve this problem faster, with divide-and-conquer or possibly through dynamically programming.
Class
public class Stock
{
public int sequenceLength;
public int[] prices;
public int lowerBound;
public int upperBound;
public int count = 0;
public Stock()
{
sequenceLength = Int32.Parse(Console.ReadLine());
prices = new int[sequenceLength];
var split = Console.ReadLine();
var splitSpace = split.Split(' ');
for (int i = 0; i < sequenceLength; i++)
prices[i] = Int32.Parse(splitSpace[i]);
lowerBound = Int32.Parse(Console.ReadLine());
upperBound = Int32.Parse(Console.ReadLine());
}
}
Usage
static void Main(string[] args)
{
int testcases = Int32.Parse(Console.ReadLine());
Stock[] stock = new Stock[testcases];
for (int i = 0; i < testcases; i++)
stock[i] = new Stock();
int count = 0;
for (int i = 0; i < stock.Length; i++)
{
for (int j = 0; j < stock[i].sequenceLength - 1; j++)
{
int sum = stock[i].prices[j];
if (sum >= stock[i].lowerBound && sum <= stock[i].upperBound)
count++;
for (int k = j + 1; k < stock[i].sequenceLength; k++)
{
sum += stock[i].prices[k];
if (sum >= stock[i].lowerBound && sum <= stock[i].upperBound)
count++;
}
}
if (stock[i].prices[stock[i].sequenceLength - 1] >= stock[i].lowerBound && stock[i].prices[stock[i].sequenceLength - 1] <= stock[i].upperBound)
count++;
stock[i].count = count;
count = 0;
}
Console.Clear();
for (int i = 0; i < stock.Length; i++)
Console.WriteLine(stock[i].count);
}
There's already an answer with O(N^2) complexity, I'll propose a O(NlogN) solution.
Create an array sums, where sums[0] = array[0] and sums[i] = sums[i-1]+array[i]. Now, for each index i in sums, you need to find number of indexes j such that sums[i] - sums[j] is in range [lower, upper]. But how to find number of indexes j?
Create a balanced binary search tree (AVL tree). Insert sums[0] in it. Start processing nodes from left to right. After processing a node, add it to the tree. You can search for the number of indexes in range [lower, upper] in O(logN) complexity, and same applies for the insertion as well. That will give you a total time complexity of O(NlogN).
If I understand the problem (and the jury is out)
The premise is, the first loop works its way across the array. The second loop is in charge of keeping a sum and checking the range, then yielding the result
Obviously this is O(n2) time complexity
Given
public static IEnumerable<int[]> GetSubs(int[] source, int lower, int upper)
{
for (var i = 0; i < source.Length; i++)
for (int j = i, sum = 0; j < source.Length; j++)
{
sum += source[j];
if (sum >= lower && sum <= upper)
yield return source[i..(j+1)];
}
}
Usage
var array = new[] { -5, -4, -3, -2, -1, 0, 2, 3, 4, 5, 6, 7, 8, 9 };
foreach (var sequence in GetSubs(array,2,5))
Console.WriteLine($"{sequence.Sum()} : [{string.Join(", ", sequence)}]");
Output
5 : [-5, -4, -3, -2, -1, 0, 2, 3, 4, 5, 6]
4 : [-4, -3, -2, -1, 0, 2, 3, 4, 5]
3 : [-3, -2, -1, 0, 2, 3, 4]
2 : [-2, -1, 0, 2, 3]
4 : [-1, 0, 2, 3]
2 : [0, 2]
5 : [0, 2, 3]
2 : [2]
5 : [2, 3]
3 : [3]
4 : [4]
5 : [5]
Full Demo Here
Note : You could probably do this in linq with Enumerable.Range, however this is pretty easy to understand
If you just wanted the count, you could remove the iterator all together, and just increment a counter when the if condition is true
public static int GetSubs(int[] source, int lower, int upper)
{
var result = 0;
for (var i = 0; i < source.Length; i++)
for (int j = i, sum = 0; j < source.Length; j++)
{
sum+= source[j];
if (sum >= lower && sum <= upper)
result++;
}
return result;
}
I want to create an subset from an array
array =[-2,1,3,-4,5]
I want the subset like this below.
[-2, 3, 5] [-2, 3] [-2, -4] [-2, 5] [1, -4] [1, 5] [3, 5]
-before the elements should be skipped one element
-2,3,5
1 -4
3.5
-then for each element, one neighbor should be skipped and the others taken one by one, forming pairs (without repeating the above)
-2,3
-2, -4
-2.5
1, -4 (do not take above have)
3.5 ((we do not have above)
-1.5
//This is my code.
static void Main(string[] args)
{
int[] kume = { -3, 4, 5, 6, 7};
String[] altkume = new string[10];
String s = "";
for(int m = 0; m<7; m++)
{
int b = m;
s += "[";
for (int j = 0; j < kume.Length; j += 2)
{
if ((b & 1) == 0)
{
s += kume[j].ToString() + ",";
}
b = b >> 1;
}
s += "]" + "\n";
altkume[m] = s;
}
for(int i = 0; i<altkume.Length; i++)
{
Console.WriteLine(altkume[i]);
}
Console.ReadKey();
}
Output:
[-3,5,7,]
[5,7,]
[-3,7,]
[7,]
[-3,5,]
[5,]
[-3,]
But i dont want like this [5,] [-3,] [7,]
I talked about problem. does anyone help ?
I've made a handy extension that will return the output you seek:
public static class Extensions
{
public static IEnumerable<List<T>> GetNonAdjacentSubsets<T>
(
this IEnumerable<T> source, // the collection we are evaluating
int min_distance, // minimum gap between numbers
List<T> subset = null // stores the progress of the subset we are evaluating
)
{
for (int i = 0; i < source.Count(); i++)
{
var new_subset = new List<T>(subset ?? Enumerable.Empty<T>())
{
source.ElementAt(i)
};
if (new_subset.Count > 1) //return subsets of more than one element
yield return new_subset;
if (source.Count() < 2) //end of list reached
yield break;
foreach (var ss in source.Skip(i + min_distance).GetNonAdjacentSubsets(min_distance, new_subset))
yield return ss;
}
}
}
Usage:
var arr = new int[] { -2, 1, 3, -4, 5 };
var subsets = new List<List<int>>(arr.GetNonAdjacentSubsets(2));
Output:
-2, 3
-2, 3, 5
-2, -4
-2, 5
1, -4
1, 5
3, 5
supposing I have a matrix like
0 -1 0 0
0 0 -1 0
0 0 0 0
0 0 -1 -1
so in this case The matrix represents:
0's are conected and -1 are not
How can I get Adjacency matrix from it?
I know
h[i][j] = 0, if there is no direct link from i to j
(i and j are not neighbors)
h[i][j] = 1, if there is a direct link from i to j
(i and j are neighbors)
so I am doing something like:
Int32[,] original = new int[4, 4]
{
{0, -1, 0, 0},
{0, 0, -1, 0},
{0, 0, 0, 0},
{0, 0, -1, -1}
}
Int32[,] adjacent;
for (int i = 0; i < original.GetLength(0); i++){
for (int j = 0; j < original.GetLength(1); j++) {
//How to know if there is direct link from i to j
//if(){
// adjacent[i,j]=0;
//}else{
// adjacent[i,j]=1;
//}
}
}
The original code has a problem - the matrixes adjacent and original are not usually the same size.
But it's close, in a way.
Code not tested:
int size = original.GetLength(0) * original.GetLength(1);
int[,] adjacent = new int[size, size];
for (int i = 0; i < original.GetLength(0); i++) {
for (int j = 0; j < original.GetLength(1); j++) {
if (original[i, j] == 0) {
// up/down
if (j > 0 && original[i, j - 1] == 0) {
adjacent[remap(i, j), remap(i, j - 1)] = 1;
adjacent[remap(i, j - 1), remap(i, j)] = 1;
}
// left/right
if (i > 0 && original[i - 1, j] == 0) {
adjacent[remap(i, j), remap(i - 1, j)] = 1;
adjacent[remap(i - 1, j), remap(i, j)] = 1;
}
}
}
}
remap maps a 2D point to a "node index". It may need more arguments. It could be something like:
int remap(int i, int j, int width)
{
return width * i + j;
}
There are other possibilities, but this is the simplest.
The adjacency matrix is an n by n matrix for a graph with n nodes (see an example here), as #harold has stated already. So you need to map between the physical (i,j) coordinates of the node in your grid, and the node number which is between 0 and n-1.
Here is some code that is along the right lines. I have looked at the output in the debugger and checking the first couple of rows it looked ok.
class Program
{
static void AddToAdjacencyMatrix(Int32[,] adjacency, Int32[,] original,
Dictionary<Tuple<int, int>, int> coordinate2NodeNum,
Tuple<int, int> fromCoord, int deltaX, int deltaY)
{
Tuple<int, int> toCoord = new Tuple<int, int>(
fromCoord.Item1 + deltaX, fromCoord.Item2 + deltaY);
try { // quick and dirty way of catching out of range coordinates
if (original[toCoord.Item1,toCoord.Item2] == 0) {
int fromNodeNum = coordinate2NodeNum[fromCoord];
int toNodeNum = coordinate2NodeNum[toCoord];
adjacency[fromNodeNum, toNodeNum] = 1;
adjacency[toNodeNum, fromNodeNum] = 1;
}
}
catch {
}
}
static void Main(string[] args)
{
Int32[,] original = new int[4, 4]
{
{0, -1, 0, 0},
{0, 0, -1, 0},
{0, 0, 0, 0},
{0, 0, -1, -1}
};
// Adjacency matrix has column and row headings for each node in graph
// Therefore we need to map between the node number in the adjacency matrix
// (i.e. the column or row heading) and the physical grid coordinates
Dictionary<int, Tuple<int, int>> nodeNum2Coordinate = new Dictionary<int, Tuple<int, int>>();
Dictionary<Tuple<int, int>, int> coordinate2NodeNum = new Dictionary<Tuple<int, int>, int>();
int nodeCount = 0;
for (int i = 0; i < original.GetLength(0); i++){
for (int j = 0; j < original.GetLength(1); j++) {
if (original[i, j] == 0) {
Tuple<int, int> coord = new Tuple<int, int>(i,j);
nodeNum2Coordinate.Add(nodeCount, coord);
coordinate2NodeNum.Add(coord, nodeCount);
nodeCount++;
}
}
}
// Now create the adacency matrix
Int32[,] adjacency = new int[nodeCount, nodeCount];
for (int i = 0; i < original.GetLength(0); i++){
for (int j = 0; j < original.GetLength(1); j++) {
if (original[i, j] == 0) {
Tuple<int, int> fromCoord = new Tuple<int, int>(i,j);
// Check connections
AddToAdjacencyMatrix(adjacency, original, coordinate2NodeNum, fromCoord,
-1, 0); // UP
AddToAdjacencyMatrix(adjacency, original, coordinate2NodeNum, fromCoord,
+1, 0); // DOWN
AddToAdjacencyMatrix(adjacency, original, coordinate2NodeNum, fromCoord,
0, -1); // LEFT
AddToAdjacencyMatrix(adjacency, original, coordinate2NodeNum, fromCoord,
0, +1); // UP
}
}
}
Console.ReadLine();
}
}