change the size of Stack in C# - c#

I write a DFS(depth first search) founction in my C# project A, and it runs OK. Then I build a new c# project B, which has more lines of codes than A. When I run the same founction in project B with the same input data,my VS2008 shows that there is a stack-overflow error.
Can I change the size of Stack in C#?
The founction's name is :FindBlocksFounction().
Codes which cause stack-overflow:
int tempx = nowx + dir[i, 0];
int tempy = nowy + dir[i, 1];
if (tempx < 0 || tempy < 0 || tempx >= m_Bitmap.Height || tempy >= m_Bitmap.Width)
continue;
int next;
next = PointList.FindIndex(t =>
{
if (t.x == tempx && t.y == tempy)
return true;
else
return false;
});//It seems like that FindIndex() in List<> costs some stack room.
if (next == -1)
continue;
if (color[next] == 0)
{
FindBlocksFounction(next);
}

I think best way to convert your Depth First Search algorithm from recursion to using Queue/Dequeue collection. It is a not complex task. Just google it or take a look here:
Non recursive Depth first search algorithm
It will prevent your code from stack size issues for any amount of data.

Related

C# loop going back and forth

I'd like to make a for loop that goes backwards after it reaches the end.
So when "i" reaches 10, instead of "i++" it should do "i--".
I've looked online for a little bit and couldn't find anything in C#. There's this post here, yet it's in JavaScript. I've rarely coded in JavaScript before, so I'm not sure how to translate that code into C# (if that even is possible).
I've successfully achieved this using the code below, yet it's lacking in the "readability" department. It also requires an extra variable, which I don't think is the most elegant solution.
Here's the code that I've written:
static void Main(string[] args)
{
bool reverse = false;
for(int i = 0; i <= 10;)
{
Console.WriteLine(i);
if(i == 10) reverse = true;
if(i == 0) reverse = false;
if(reverse == false) i++;
if(reverse == true) i--;
// Slow down the output
Thread.Sleep(20);
}
}
Again, this code works fine, yet I'm looking for a better solution than this mess.
Thanks in advance!
As small readability improvement I would suggest to change from the boolean flag to an integer increment value:
int increment = 1;
for (int i = 0; i >= 0 && i <= 10; i += increment)
{
Console.WriteLine(i);
if (i == 10) increment = -1;
else if (i == 0) increment = 1;
}

Ask someone's help to explain the performance issue of C# code

I am a C# developer and I am training my coding and algorithm skills on LeetCode.
And now I am handling the problem 5: longest palindromic substring.
I want someone can explain the reason.
My solution version 1 to this problem was:
public string LongestPalindrome(string s)
{
// Step 0: Handles invalid or special cases.
if (string.IsNullOrWhiteSpace(s) ||
s.Length == 1 ||
s.Distinct().Count() == 1 ||
s.Reverse().SequenceEqual(s))
{
return s;
}
if (s.Length == 2)
{
return s.First().Equals(s.Last()) ? s : s.First().ToString();
}
if (s.Distinct().Count() == s.Length)
{
return s.First().ToString();
}
// Step 1: Handles normal cases.
var longestPalindromeSubstring = string.Empty;
for (var index = 0; index < s.Length && s.Length - index > longestPalindromeSubstring.Length; index++)
{
var currentChar = s[index];
var currentCharLastIndex = s.LastIndexOf(currentChar);
if (index == currentCharLastIndex)
{
if (!string.IsNullOrWhiteSpace(longestPalindromeSubstring) ||
longestPalindromeSubstring.Length > 1)
{
continue;
}
longestPalindromeSubstring = currentChar.ToString();
}
var currentCharIndexes = new Stack<int>();
for (var nextIndex = index + 1; nextIndex <= currentCharLastIndex; nextIndex++)
{
if (s[nextIndex] == currentChar)
{
currentCharIndexes.Push(nextIndex);
}
}
while (currentCharIndexes.Any())
{
var relatedIndex = currentCharIndexes.Pop();
var possibleStr = s.Substring(index, relatedIndex - index + 1);
var reversedPossibleStr = new string(possibleStr.Reverse().ToArray());
if (!possibleStr.Equals(reversedPossibleStr) ||
possibleStr.Length < longestPalindromeSubstring.Length ||
possibleStr.Equals(longestPalindromeSubstring))
{
continue;
}
longestPalindromeSubstring = possibleStr;
}
}
return longestPalindromeSubstring;
}
However this solution above was failed to pass the LeetCode validation since the issue: Time Limit Exceeded.
Then I just made a small update, and the solution version 2 passed, the changed part was only adding ToCharArray() method before invoking Reverse() method:
var reversedPossibleStr = new string(possibleStr.ToCharArray().Reverse().ToArray());
if (!possibleStr.Equals(reversedPossibleStr) ||
possibleStr.Length < longestPalindromeSubstring.Length ||
possibleStr.Equals(longestPalindromeSubstring))
{
continue;
}
…………
But I am not sure the reason why it can work, I just guessed that the data in an array will be arranged in a sequence memory space, it may help to improve the performance, could someone explain more detail.
Thank you in advance.
The Reverse method uses EnumerableHelpers.ToArray to fetch the count of the input enumerable. If the enumerable doesn't implement ICollection<T> interface, it will use a list-like approach to creates an array which will extend the array many times. Unfortunately string doesn't implement ICollection<char>, though it knows how many characters it contains, so string.Reverse() is slower than string.ToCharArray().Reverse().

Is this Levenshtein Distance algorithm correct?

I have written the algorithm below to compute the Levenshtein distance, and it seems to return the correct results based on my tests. The time complexity is O(n+m), and the space is O(1).
All the existing algorithms I've seen only for this have space complexity O(n*m), as they create a matrix. Is there something wrong with my algorithm?
public static int ComputeLevenshteinDistance(string word1, string word2)
{
var index1 = 0;
var index2 = 0;
var numDeletions = 0;
var numInsertions = 0;
var numSubs = 0;
while (index1 < word1.Length || index2 < word2.Length)
{
if (index1 == word1.Length)
{
// Insert word2[index2]
numInsertions++;
index2++;
}
else if (index2 == word2.Length)
{
// Delete word1[index1]
numDeletions++;
index1++;
}
else if (word1[index1] == word2[index2])
{
// No change as word1[index1] == word2[index2]
index1++;
index2++;
}
else if (index1 < word1.Length - 1 && word1[index1 + 1] == word2[index2])
{
// Delete word1[index1]
numDeletions++;
index1++;
}
else if (index2 < word2.Length - 1 && word1[index1] == word2[index2 + 1])
{
// Insert word2[index2]
numInsertions++;
index2++;
}
else
{
// Substitute word1[index1] for word2[index2]
numSubs++;
index1++;
index2++;
}
}
return numDeletions + numInsertions + numSubs;
}
Was a comment, but I feel it is probably suitable as an answer:
Short answer is "no", if you want the true shortest distance for any given inputs.
The reason your code appears more efficient (and the reason that other implementations create a matrix instead of doing what you're doing) is that your stepwise implementation ignores a lot of potential solutions.
Examples #BenVoigt gave illustrate this, another perhaps clearer illustration is ("aaaardvark", "aardvark") returns 8, should be 2: it's getting tripped up because it's matching the first a and thinking it can move on, when in fact a more optimal solution would be to consider the first two characters insertions.

How to skip "0" from a for() statement [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
How do i skip the number 0 (zero) from this for() statement. I want 0 to be skipped after -1.
for(x=min_values; x<= max_values; x++)
{
}
my aim is to select every number between two textboxes called max and min. I am trying to plot a y = 1/x function. However when 0 is selected because there can not be 1/0 the program does not work.
The 'continue' keyword would accomplish what you're asking given the right logical condition. The example code you gave doesn't say what mix_values or max_values is.
for(int x = mix_values; x <= max_values; x++ ) {
if( x == 0 ) { continue; }
// ...the rest of your loop body logic goes here...
}
I'm assuming 0 would always come after -1 (if min_values < -1) given x++.
Try this inside the for :
if (x == 0 && lastMinValue == -1 ){
continue;
}
int lastMinValue = x;
I'm going to assume that min_values may be negative. You've said:
I want 0 to be skipped after -1.
In that case, just increment x at the end of the loop body when it's -1:
for(x=min_values; x<= max_values; x++)
{
// ...loop body here, then:
if (x == -1) {
++x;
}
}
After that increment, the one in the for loop will kick in and the next x will be 1.
I won't have any effect if min_values is 0, because you said you wanted to skip 0 after -1, suggesting some kind of dependent relationship.
Example in C# on IDEone, and here in JavaScript, which in this case (not generally) works the same way as C#:
var min_values = -2;
var max_values = 2;
var x;
for (x = min_values; x <= max_values; x++) {
console.log(x);
if (x == -1) {
++x;
}
}
Of course, if you just wanted to skip 0, you'd do what was suggested in a comment that you said didn't work:
for(x=min_values; x<= max_values; x++)
{
if (x == 0) {
continue;
}
// ...loop body here
}
...because it does work: Example in C# on IDEone or, again, here in JavaScript as it happens to work the same way in this regard:
var min_values = -2;
var max_values = 2;
var x;
for (x = min_values; x <= max_values; x++) {
if (x == 0) {
continue;
}
console.log(x);
}

Sudoku algorithm in C#

I need one liner (or close to it) that verifies that given array of 9 elements doesn't contain repeating numbers 1,2,3,...,9. Repeating zeroes do not count (they represent empty cells).
The best I have came out so far is:
var a = new int[9] {1,2,3,4,5,6,7,8,9};
var itIsOk = a.Join(a, i => i, j => j, (x, y) => x)
.GroupBy(y => y).Where(g => g.Key > 0 && g.Count() > 1).Count() == 0;
If you don't want to solve my problems :), could you at least tell if the above algorithm works correctly?
And, yes, a have read this one.
This is about 50-250 times faster than a LINQ solution (depending on how early the duplicate is found):
public static bool IsValid(int[] values) {
int flag = 0;
foreach (int value in values) {
if (value != 0) {
int bit = 1 << value;
if ((flag & bit) != 0) return false;
flag |= bit;
}
}
return true;
}
Lucky for you I built a sudoku solver myself not too long ago :) The whole thing was about 200 lines of C#, and it would solve the toughest puzzles I could find line in 4 seconds or less.
Performance probably isn't that great due to the use of .Count, but it should work:
!a.Any(i => i != 0 && a.Where(j => j != 0 && i == j).Count > 1)
Also, the j != 0 part isn't really needed, but it should help things run a bit faster.
[edit:] kvb's answer gave me another idea:
!a.Where(i => i != 0).GroupBy(i => i).Any(gp => gp.Count() > 1)
Filter the 0's before grouping. Though based on how IEnumerable works it may not matter any.
Either way, For best performance replace .Count > 1 in either of those with a new IEnumerable extension method that looks like this:
bool MoreThanOne(this IEnumerable<T> enumerable, Predictate<T> pred)
{
bool flag = false;
foreach (T item in enumerable)
{
if (pred(item))
{
if (flag)
return true;
flag = true;
}
}
return false;
}
It probably won't matter too much since arrays are limited to 9 items, but if you call it a lot it might add up.
!a.GroupBy(i => i).Any(gp => gp.Key != 0 && gp.Count() > 1)
This is an old question, but I recently was pointed to a 1 line solution using Oracle's custom SQL for doing tree-like structures. I thought it would be nice to convert this into Linq.
You can read more on my blog about how to Solve Sudoku in 1 line of Linq
Here is the code:
public static string SolveStrings(string Board)
{
string[] leafNodesOfMoves = new string[] { Board };
while ((leafNodesOfMoves.Length > 0) && (leafNodesOfMoves[0].IndexOf(' ') != -1))
{
leafNodesOfMoves = (
from partialSolution in leafNodesOfMoves
let index = partialSolution.IndexOf(' ')
let column = index % 9
let groupOf3 = index - (index % 27) + column - (index % 3)
from searchLetter in "123456789"
let InvalidPositions =
from spaceToCheck in Enumerable.Range(0, 9)
let IsInRow = partialSolution[index - column + spaceToCheck] == searchLetter
let IsInColumn = partialSolution[column + (spaceToCheck * 9)] == searchLetter
let IsInGroupBoxOf3x3 = partialSolution[groupOf3 + (spaceToCheck % 3) +
(int)Math.Floor(spaceToCheck / 3f) * 9] == searchLetter
where IsInRow || IsInColumn || IsInGroupBoxOf3x3
select spaceToCheck
where InvalidPositions.Count() == 0
select partialSolution.Substring(0, index) + searchLetter + partialSolution.Substring(index + 1)
).ToArray();
}
return (leafNodesOfMoves.Length == 0)
? "No solution"
: leafNodesOfMoves[0];
}
Why do you want a convoluted line of Linq code, rather than wrapping up an efficient implementation of the test in an extension method and calling that?
var a = new int[9] {1,2,3,4,5,6,7,8,9};
var itIsOk = a.HasNoNonZeroRepeats();
One implementation of NoNonZeroRepeats could be to use the 9 lowest bits of a short to indicate presence of a value in the array, giving an O(length(a)) test with no dynamic memory use. Linq is cute, but unless you're only using it for aesthetic reasons (you don't specifically say that you're writing a sudoku solver using only Linq as an exercise) it seems to be just adding complexity here.
How about:
var itIsOk = a.Where(x => x > 0).Distinct().Count() == a.Where(x => x > 0).Count();
Reasoning: First create an enumeration without 0s. Out of the remaining numbers, if its distinct list is the same length as the actual list, then there are no repeats.
or:
If the list of unique numbers is smaller than the actual list, then you must have a repeated number.
This is the one-liner version. The a.Where(x=>x>0) list could be factored out.
var nonZeroList = a.Where(x => x > 0).ToList();
var itIsOk = nonZeroList.Distinct().Count() == nonZeroList.Count();
I usually frown on solutions that involve captured variables, but I had an urge to write this:
bool hasRepeating = false;
int previous = 0;
int firstDuplicateValue = a
.Where(i => i != 0)
.OrderBy(i => i)
.FirstOrDefault(i =>
{
hasRepeating = (i == previous);
previous = i;
return hasRepeating;
});
if (hasRepeating)
{
Console.WriteLine(firstDuplicateValue);
}
For brevity if not performance, how about
var itIsOk = a.Sum() == a.Distinct().Sum();
The following is simple and fast.
if a.Select(i => Math.Pow(2, i - 1)).ToArray().Sum()==511 ...

Categories

Resources