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,

Slides:



Advertisements
Similar presentations
© 2004 Goodrich, Tamassia Depth-First Search1 DB A C E.
Advertisements

CS 1031 Graphs Definition of Graphs and Related Concepts Representation of Graphs The Graph Class Graph Traversal Graph Applications.
CS203 Lecture 15.
CSE 390B: Graph Algorithms Based on CSE 373 slides by Jessica Miller, Ruth Anderson 1.
Depth-First Search1 Part-H2 Depth-First Search DB A C E.
Graphs CSC 220 Data Structure. Introduction One of the Most versatile data structures like trees. Terminology –Nodes in trees are vertices in graphs.
Graph Theory, DFS & BFS Kelly Choi What is a graph? A set of vertices and edges –Directed/Undirected –Weighted/Unweighted –Cyclic/Acyclic.
CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
Graph Searching CSE 373 Data Structures Lecture 20.
Sociology and CS Philip Chan. How close are people connected? Are people closely connected, not closely connected, isolated into groups, …
Breadth-First and Depth-First Search
© 2006 Pearson Addison-Wesley. All rights reserved14 A-1 Chapter 14 excerpts Graphs (breadth-first-search)
CS 206 Introduction to Computer Science II 11 / 07 / 2008 Instructor: Michael Eckmann.
Graphs Graphs are the most general data structures we will study in this course. A graph is a more general version of connected nodes than the tree. Both.
Data Structure and Algorithms (BCS 1223) GRAPH. Introduction of Graph A graph G consists of two things: 1.A set V of elements called nodes(or points or.
Algoritma Traversal Graph. Graph Traversal Algorithms In general, graphs do not have a vertex, like a root, that initiates unique paths to each of the.
CSC 331: Algorithm Analysis Paths in Graphs. The DFS algorithm we gave is recursive. DFS generates a search tree showing paths from one vertex to all.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 21: Graphs.
Graphs CS-240/341. Graphs Used for representing many-to-many relationships –can take two forms directed (digraph) - a finite set of elements called vertices.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
Graphs CS-240/341. Uses for Graphs computer networks and routing airline flights geographic maps course prerequisite structures tasks for completing a.
CS 206 Introduction to Computer Science II 11 / 10 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 11 / 03 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 03 / 25 / 2009 Instructor: Michael Eckmann.
Breadth First Search (BFS) Part 2 COMP171. Graph / Slide 2 Shortest Path Recording * BFS we saw only tells us whether a path exists from source s, to.
Graphs & Graph Algorithms Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
CS 206 Introduction to Computer Science II 03 / 30 / 2009 Instructor: Michael Eckmann.
Tirgul 7 Review of graphs Graph algorithms: – BFS (next tirgul) – DFS – Properties of DFS – Topological sort.
Review of Graphs A graph is composed of edges E and vertices V that link the nodes together. A graph G is often denoted G=(V,E) where V is the set of vertices.
C o n f i d e n t i a l HOME NEXT Subject Name: Data Structure Using C Unit Title: Graphs.
CS261 Data Structures DFS and BFS – Edge List Representation.
Search Related Algorithms. Graph Code Adjacency List Representation:
MA/CSSE 473 Day 12 Insertion Sort quick review DFS, BFS Topological Sort.
Representing and Using Graphs
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
Graphs. Made up of vertices and arcs Digraph (directed graph) –All arcs have arrows that give direction –You can only traverse the graph in the direction.
Graph Traversal BFS & DFS. Review of tree traversal methods Pre-order traversal In-order traversal Post-order traversal Level traversal a bc d e f g hi.
Graphs. Definitions A graph is two sets. A graph is two sets. –A set of nodes or vertices V –A set of edges E Edges connect nodes. Edges connect nodes.
COMP261 Lecture 6 Dijkstra’s Algorithm. Connectedness Is this graph connected or not? A Z FF C M N B Y BB S P DDGG AA R F G J L EE CC Q O V D T H W E.
Graphs – Part II CS 367 – Introduction to Data Structures.
CSS446 Spring 2014 Nan Wang.  To understand the implementation of linked lists and array lists  To analyze the efficiency of fundamental operations.
Graph Introduction, Searching Graph Theory Basics - Anil Kishore.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 13: Graphs Data Abstraction & Problem Solving with C++
Graphs Graphs are collections of vertices and edges. Vertices are simple objects with associated names and other properties. Edges are connections between.
1 Directed Graphs Chapter 8. 2 Objectives You will be able to: Say what a directed graph is. Describe two ways to represent a directed graph: Adjacency.
COSC 2007 Data Structures II
7. Graph Traversal Describe and compare depth-first and breadth-first graph searching, and look at the creation of spanning trees Contest Algorithms: 7.
Breadth-first and depth-first traversal Prof. Noah Snavely CS1114
Graphs and Paths : Chapter 15 Saurav Karmakar
Breadth-first and depth-first traversal CS1114
Graph Searching CSIT 402 Data Structures II. 2 Graph Searching Methodology Depth-First Search (DFS) Depth-First Search (DFS) ›Searches down one path as.
CS 367 Introduction to Data Structures Lecture 13.
GRAPH ALGORITHM. Graph A pair G = (V,E) – V = set of vertices (node) – E = set of edges (pairs of vertices) V = (1,2,3,4,5,6,7) E = ( (1,2),(2,3),(3,5),(1,4),(4,5),(6,7)
Lecture #13. Topics 1.The Graph Abstract Data Type. 2.Graph Representations. 3.Elementary Graph Operations.
Graphs. What is a graph? In simple words, A graph is a set of vertices and edges which connect them. A node (or vertex) is a discrete position in the.
Nattee Niparnan. Graph  A pair G = (V,E)  V = set of vertices (node)  E = set of edges (pairs of vertices)  V = (1,2,3,4,5,6,7)  E = ((1,2),(2,3),(3,5),(1,4),(4,
Code: BCA302 Data Structures with C Prof. (Dr.) Monalisa Banerjee By.
Review Graph Directed Graph Undirected Graph Sub-Graph Spanning Sub-Graph Degree of a Vertex Weighted Graph Elementary and Simple Path Link List Representation.
Data Structures & Algorithm Analysis lec(8):Graph T. Souad alonazi
Graphs Chapter 15 introduces graphs which are probably the most general and commonly-used data structure. This lecture introduces heaps, which are used.
Graphs Representation, BFS, DFS
Data Structures and Algorithms for Information Processing
Graphs Representation, BFS, DFS
What is a Graph? a b c d e V= {a,b,c,d,e} E= {(a,b),(a,c),(a,d),
Chapter 11 Graphs.
CMSC 202 Trees.
Graph Implementation.
Presentation transcript:

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, -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);

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 =

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

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

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

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

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

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

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