3

I am trying to implement an adjacency list in Swift, basically a collection of lists, or I guess a collection of mutable arrays. I wrote a Depth Search First implementation in Java for and I would like to write the same in Swift. This code of for the Graph representation. Any ideas of how to do this? Here is the code in Java if that helps:

public class Graph
{

    private List<Integer>[] adj;

    public Graph(int numberOfVertices)
    {
         adj = (List<Integer>[]) new List[numberOfVertices];
         ...
         for(int i = 0; i < numberOfVertices; i++)
               adj[i] = new ArrayList<Integer>(); 

    }

Even knowing how to declare a variable that contains an array of mutable arrays would be great. I didn't find any information on how to do this in the book released by apple or the web.

2
  • A work around is to use AnyObject[] for your root array, then you can add new array to that array Commented Jun 14, 2014 at 1:10
  • @LongNV There is no reason to use a type so generic as AnyObject. You can specify the exact type of the arrays within the array Commented Jun 14, 2014 at 2:40

2 Answers 2

6

You can declare an array of arrays simply by doing the following:

var nestedArrays : [[Int]] = []
nestedArrays.append([1, 2])
nestedArrays[0].append(3)
nestedArrays // [[1, 2, 3]]

If you wanted to have it start with a number of empty arrays you could do the following:

var nestedArrays = [[Int]](count: 10, repeatedValue: [])

Then it would start with 10 empty arrays

Your class could look like this:

class Graph {
    var adj : [[Int]]

    init(numberOfVertices: Int) {
        adj = [[Int]](count: numberOfVertices, repeatedValue: [])
    }
}

Or if you would like to create a class that can have a graph of any type of object you can use generics:

class Graph<T> {
    var adj : [[T]]

    init(numberOfVertices: Int) {
        adj = [[T]](count: numberOfVertices, repeatedValue: [])
    }
}
Sign up to request clarification or add additional context in comments.

Comments

2

This is the Swift equivalent to your code:

class Graph {
    var adj : Array<Array<Int>>

    init (_ numberOfVertices: Int) {
        adj = Array<Array<Int>> (count: numberOfVertices, repeatedValue: [])
        for i in 1..numberOfVertices {
            adj[i] = Array<Int> ()
        }
    }
}

and some 'tests':

 11> var gr = Graph(5)
gr: Graph = {
  adj = size=5 {
    [0] = size=0
    [1] = size=0
    [2] = size=0
    [3] = size=0
    [4] = size=0
  }
}
 12> gr.adj[1].append (2)
 13> gr.adj[1].append (10)
 ...
 15> gr.adj[4].append (7)
 16> gr.adj
$R5: Int[][] = size=5 {
  [0] = size=0
  [1] = size=2 {
    [0] = 2
    [1] = 10
  }
  [2] = size=0
  [3] = size=0
  [4] = size=1 {
    [0] = 7
  }
}
 17> gr.adj[4][0]
$R7: Int = 7

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.