Presentation is loading. Please wait.

Presentation is loading. Please wait.

Where’s the title? You gotta search for it!. PotW Solution String s = new Scanner(System.in).next(); int[] prev = new int[s.length() * 2 + 2]; Arrays.fill(prev,

Similar presentations


Presentation on theme: "Where’s the title? You gotta search for it!. PotW Solution String s = new Scanner(System.in).next(); int[] prev = new int[s.length() * 2 + 2]; Arrays.fill(prev,"— Presentation transcript:

1 Where’s the title? You gotta search for it!

2 PotW Solution String s = new Scanner(System.in).next(); int[] prev = new int[s.length() * 2 + 2]; Arrays.fill(prev, -1); int c = s.length(), ans = 0; for (int i = 0; i < s.length(); i++) { if (s.charAt(i) == '(') { if (prev[c] == -1) prev[c] = i; c++; } else { prev[c] = -1; c--; if (prev[c] != -1) ans = Math.max(ans, i - prev[c] + 1); } System.out.println(ans);

3 Graphs Graph = collection of nodes and edges Connected component = path from every node to every other node Distance = # of edges from one node to another Can be represented as adjacency list (e.g. array of ArrayLists) – list of adjacent nodes for each node =

4 Search Algorithms An algorithm to find an item with certain properties among a collection o Lowest/highest value, closest distance, etc. Search trees – Tree with data values in nodes stored in such a way that they can be traversed in order in some systematic way

5 Floodfill Given a m by n grid of colored tiles, find connected regions of black tiles Analogous to a graph where the nodes are tiles and edges are adjacencies between tiles This was used in the November bronze/silver contests o Beauty Pageant problem is reduced to a connectivity problem (floodfill) + finding distances between connected regions Solve floodfill using Depth First Search (DFS) o Recursively expand nodes o Keep expanding until you hit a dead end

6 Depth First Search (DFS) boolean[] visited // initialized to false dfs(current) if current equals end // end found exit dfs mark current as visited for all neighbors of current if neighbor is not visited dfs(neighbor) Sometimes useful to incorporate an "end" node Might also be convenient to give DFS a return value of true/false Denotes whether end was found Java's stack memory limit is very unforgiving Replace recursion with a Stack if the number of nodes exceeds a few thousand

7 Finding Distances DFS doesn't quite cut it if you're looking for distances o DFS explores the nodes in a head on, haphazard fashion Opt for Breadth First Search (BFS) o Start from nodes of distance 0, expand to distance 1, etc., basically exploring in order of distance o Only works if all edges have length 1 o Use a queue - nodes closer are explored first o Note that this simply replaces the stack used in DFS with a queue o Not quite as efficient or easy to code as DFS, so only use BFS if distances are needed

8 Breadth First Search (BFS) boolean[] visited // initialized to false int[] dist bfs(start) Queue Q visit start, set distance to zero push start into Q while Q is not empty pop first element of Q for all neighbors of the first element if neighbor not visited visit neighbor, set distance of neighbor add neighbor to queue Save distances in a dist array o Set distance of neighbor to (current distance + 1) Java has a Queue interface, but underneath you still need to use LinkedList, which implements Queue o Can do the same for Stack in DFS

9 PotW – Friendship Trees This problem has nothing to do with cows, but for some reason cows are nodes, and friendships are edges. Check if the graph is a tree. Each edge is undirected, since friendships are mutual (hopefully) A tree is a graph such that all nodes are connected and there is only one unique way to get between each two nodes

10 Friendship Trees (cont.) The first line of the input contains the number of nodes and then the number of edges Each successive line contains an edge, and has two indices, where indices are numbered starting from one The output should be either "YES" or "NO" Sample Input: Sample Output: 3 2 YES 1 2 2 3 1 2 3


Download ppt "Where’s the title? You gotta search for it!. PotW Solution String s = new Scanner(System.in).next(); int[] prev = new int[s.length() * 2 + 2]; Arrays.fill(prev,"

Similar presentations


Ads by Google