Topological Sorting

Topological Sorting

Given an directed graph, a topological order of the graph nodes is defined as follow:

  • For each directed edge A -> B in graph, A must before B in the order list.
  • The first node in the order can be any node in the graph with no nodes direct to it.

Find any topological order for the given graph.

Example

For graph as follow:

picture

The topological order can be:

<code>[0, 1, 2, 3, 4, 5]
[0, 2, 3, 1, 5, 4]
...
</code>
Note

You can assume that there is at least one topological order in the graph.

Challenge

Can you do it in both BFS and DFS?

Solution 1. BFS

1. first create a map which contains all the nodes and its indegrees

2. only add node with 0 indegree to the list and once the node is select, remove 1 indegree from all its neighbors

public ArrayList<DirectedGraphNode> topSort(ArrayList<DirectedGraphNode> graph) {
    //BFS
    ArrayList<DirectedGraphNode> result = new ArrayList<>();
    if (graph == null || graph.size() <= 1) {
        return graph;
    }
    HashMap<DirectedGraphNode, Integer> inDegreeMap = new HashMap<>();
    for (DirectedGraphNode node : graph) {
        if (!inDegreeMap.containsKey(node)) {
            inDegreeMap.put(node, 0);
        }
        for (DirectedGraphNode neighbor : node.neighbors) {
            if (inDegreeMap.containsKey(neighbor)) {
                inDegreeMap.put(neighbor, inDegreeMap.get(neighbor) + 1);
            } else {
                inDegreeMap.put(neighbor, 1);
            }
        }
    }
    while (!inDegreeMap.isEmpty()) {
        for (DirectedGraphNode node : inDegreeMap.keySet()) {
            if (inDegreeMap.get(node) == 0) {
                result.add(node);
                inDegreeMap.remove(node);
                for (DirectedGraphNode neighbor : node.neighbors) {
                    inDegreeMap.put(neighbor, inDegreeMap.get(neighbor) - 1);
                }
                break;
            }
        }
    }
    return result;
}
FacebookTwitterGoogle+Share

Combination Sum

Combination Sum

Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.

The same repeated number may be chosen from C unlimited number of times.

For example, given candidate set 2,3,6,7 and target 7,
A solution set is:
[7]
[2, 2, 3]

Example

given candidate set 2,3,6,7 and target 7,
A solution set is:
[7]
[2, 2, 3]

Note

  • All numbers (including target) will be positive integers.
  • Elements in a combination (a1, a2, … , ak) must be in non-descending order. (ie, a1 ≤ a2 ≤ … ≤ ak).
  • The solution set must not contain duplicate combinations.

Solution: Subsets 变种题 DFS

O(2^n) time

public List<List<Integer>> combinationSum(int[] candidates, int target) {
    List<List<Integer>> result = new ArrayList<>();
    if (candidates == null) {
        return result;
    }
    Arrays.sort(candidates);
    combinationSumHelper(new ArrayList<Integer>(), 0, candidates, target, result);

    return result;
}

public void combinationSumHelper(ArrayList<Integer> curr, int index, int[] candidates, int target, List<List<Integer>> result) {
    if (target == 0) {
        result.add(new ArrayList<>(curr));
        return;
    }
    if (index >= candidates.length) {
        return;
    }
    for (int i = index; i < candidates.length; i++) {
        if (candidates[i] <= target) {
            curr.add(candidates[i]);
            combinationSumHelper(curr, i, candidates, target - candidates[i], result);
            curr.remove(curr.size() - 1);
        }
    }
}