Search code examples
arraysif-statementpseudocode

How does this if statement work? It appears to have no boolean expression


This is a snippet of code. I am unsure how the if statement on the second last line works? It doesn't have a boolean expression like 'if A[n − 1, j] == x' or anything like that. Could someone explain what it actually does to me? Thanks

Connected(A[0..n − 1, 0..n − 1])

  //Input: Adjacency matrix A[0..n − 1, 0..n − 1]) of an undirected graph G

  //Output: 1 (true) if G is connected and 0 (false) if it is not

if n = 1 return 1 //one-vertex graph is connected by definition

else

 if not Connected(A[0..n − 2, 0..n − 2]) return 0

 else for j ←0 to n − 2 do
          if A[n − 1, j] return 1
      return 0

Solution

  • It calls for the A function which returns a boolean (0 or 1), so that's how it works.

    Then, when your code executes A[n − 1, j], it will call the A function, which is the function that you're defining there and it will (eventually) return your boolean

    This is a recursive function. You'll see lots of them and finnally understand it. In fact, is simple, it is a function that calls itself many times, each one with different parameters, until one of the calls truly returns a result which is passed all the way back through all the chain of functions