Programming Practicum Day 3: Problem Solving with Graphs Aaron Tan NUS School of Computing.

Slides:



Advertisements
Similar presentations
Lecture 15. Graph Algorithms
Advertisements

Cpt S 223 – Advanced Data Structures Graph Algorithms: Introduction
PSU CS Algorithms Analysis and Design Graphs.
Graphs Chapter 12. Chapter Objectives  To become familiar with graph terminology and the different types of graphs  To study a Graph ADT and different.
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.
Prim’s Algorithm from a matrix A cable TV company is installing a system of cables to connect all the towns in the region. The numbers in the network are.
Maths for Computer Graphics
Graph & BFS.
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.
Multidimensional arrays Many problems require information be organized as a two- dimensional or multidimensional list Examples –Matrices –Graphical animation.
Chapter 9 Graph algorithms. Sample Graph Problems Path problems. Connectedness problems. Spanning tree problems.
CS 206 Introduction to Computer Science II 10 / 31 / 2008 Happy Halloween!!! Instructor: Michael Eckmann.
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.
Graphs. Graph definitions There are two kinds of graphs: directed graphs (sometimes called digraphs) and undirected graphs Birmingham Rugby London Cambridge.
Introduction to Graphs
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
CS 206 Introduction to Computer Science II 11 / 03 / 2008 Instructor: Michael Eckmann.
Chapter 9 Graph algorithms Lec 21 Dec 1, Sample Graph Problems Path problems. Connectedness problems. Spanning tree problems.
CSE 780 Algorithms Advanced Algorithms Graph Algorithms Representations BFS.
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.
CS 206 Introduction to Computer Science II 11 / 09 / 2009 Instructor: Michael Eckmann.
Graphs G = (V,E) V is the vertex set. Vertices are also called nodes and points. E is the edge set. Each edge connects two different vertices. Edges are.
Fall 2007CS 2251 Graphs Chapter 12. Fall 2007CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs To.
Graph Operations And Representation. Sample Graph Problems Path problems. Connectedness problems. Spanning tree problems.
Graph. Data Structures Linear data structures: –Array, linked list, stack, queue Non linear data structures: –Tree, binary tree, graph and digraph.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
1 Chapter 28 Weighted Graph Applications. 2 Objectives F To represent weighted edges using adjacency matrices and priority queues (§28.2). F To model.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 28 Weighted Graph.
Graph Dr. Bernard Chen Ph.D. University of Central Arkansas.
GRAPHS CSE, POSTECH. Chapter 16 covers the following topics Graph terminology: vertex, edge, adjacent, incident, degree, cycle, path, connected component,
Graphs. What is a graph? A data structure that consists of a set of nodes (vertices) and a set of edges that relate the nodes to each other The set of.
Module 5 – Networks and Decision Mathematics Chapter 23 – Undirected Graphs.
 What is a graph? What is a graph?  Directed vs. undirected graphs Directed vs. undirected graphs  Trees vs graphs Trees vs graphs  Terminology: Degree.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
1 CS104 : Discrete Structures Chapter V Graph Theory.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
CS1101: Programming Methodology Aaron Tan.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
Fundamental Data Structures and Algorithms (Spring ’05) Recitation Notes: Graphs Slides prepared by Uri Dekel, Based on recitation.
Matrices Section 2.6. Section Summary Definition of a Matrix Matrix Arithmetic Transposes and Powers of Arithmetic Zero-One matrices.
© Nuffield Foundation 2012 Nuffield Free-Standing Mathematics Activity Networks © Rudolf Stricker.
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
Data Structures & Algorithms Graphs Richard Newman based on book by R. Sedgewick and slides by S. Sahni.
Graphs G = (V,E) V is the vertex set. Vertices are also called nodes and points. E is the edge set. Each edge connects two different vertices. Edges are.
Graphs 황승원 Fall 2010 CSE, POSTECH. 2 2 Graphs G = (V,E) V is the vertex set. Vertices are also called nodes and points. E is the edge set. Each edge connects.
GRAPHS. Graph Graph terminology: vertex, edge, adjacent, incident, degree, cycle, path, connected component, spanning tree Types of graphs: undirected,
Discrete Structures CISC 2315 FALL 2010 Graphs & Trees.
Graphs Upon completion you will be able to:
Graph Theory. undirected graph node: a, b, c, d, e, f edge: (a, b), (a, c), (b, c), (b, e), (c, d), (c, f), (d, e), (d, f), (e, f) subgraph.
Chapter 9: Graphs.
Graphs Definition: a graph is an abstract representation of a set of objects where some pairs of the objects are connected by links. The interconnected.
Chapter 05 Introduction to Graph And Search Algorithms.
Graphs. Graph Definitions A graph G is denoted by G = (V, E) where  V is the set of vertices or nodes of the graph  E is the set of edges or arcs connecting.
Lecture 20. Graphs and network models 1. Recap Binary search tree is a special binary tree which is designed to make the search of elements or keys in.
Matrices. Matrix A matrix is an ordered rectangular array of numbers. The entry in the i th row and j th column is denoted by a ij. Ex. 4 Columns 3 Rows.
Subject Four Graphs Data Structures. What is a graph? A data structure that consists of a set of nodes (vertices) and a set of edges that relate the nodes.
12-1 Organizing Data Using Matrices
Graphs Representation, BFS, DFS
Matrix Representation of Graphs
15. Directed graphs and networks
Minimum Spanning Tree Chapter 13.6.
CS200: Algorithm Analysis
Lecture 19-Problem Solving 4 Incremental Method
Graph Operations And Representation
CSCI2100 Data Structures Tutorial
Prim’s Minimum Spanning Tree Algorithm Neil Tang 4/1/2008
Graphs G = (V,E) V is the vertex set.
Chapter 9 Graph algorithms
Presentation transcript:

Programming Practicum Day 3: Problem Solving with Graphs Aaron Tan NUS School of Computing

2 Contents Review of Day 2 problems Graphs [Programming Practicum, December 2009]

3 Day 2 Ex 1: Max. Subseq. Sum (1/3) Given list 3 [Programming Practicum, December 2009] Answer = 11

4 Day 2 Ex 1: Max. Subseq. Sum (2/3) An algorithm 4 [Programming Practicum, December 2009] What is the time complexity? public static int maxSubseqSum(int[] arr) { int sum, maxSum; maxSum = 0; for (int i=0; i<arr.length; i++) { sum = 0; for (int j=i; j<arr.length; j++) { sum += arr[j]; if (sum > maxSum) maxSum = sum; } return maxSum; }

5 Day 2 Ex 1: Max. Subseq. Sum (3/3) Another algorithm 5 [Programming Practicum, December 2009] What is the time complexity? public static int maxSubseqSum(int[] arr) { int sum, maxSum; sum = maxSum = 0; for (int i=0; i<arr.length; i++) { sum += arr[i]; if (sum < 0) sum = 0; else if (sum > maxSum) maxSum = sum; } return maxSum; }

6 Day 2 Ex 3: Finding k th Smallest Element Belongs to the classic selection problem Many algorithms available We adapt the heapsort:  Create min-heap instead of max-heap  After heapify, instead of swapping the top elements n-1 times (n = array size), we need only to swap it k-1 times and sift-down.  Heapify: O(n)  Swapping k-1 times and sift-down: O(k lg n). If k < n/(lg n), then this is O(n) as well. 6 [Programming Practicum, December 2009]

7 Graphs A graph consists of a set of nodes (vertices) connected by edges. Undirected graphs: edges are undirected. Directed graphs: directed edges from a node to another. Edges may be weighted, that is, each of them contains a value (weight). Graph is a very important data structure that supports many applications (Shortest-path, minimum spanning tree, etc.) [Programming Practicum, December 2009]

8 Graphs: Degrees Directed graphs:  In-degree of a node: Number of edges pointing towards that node  Out-degree of a node: Number of edges pointing away from that node. Undirected graphs:  Degree of a node: Number of edges connected to that node. [Programming Practicum, December 2009]

9 Graphs: Example A road network, where nodes represent cities and edges represent costs (distance, or time). [Programming Practicum, December 2009] A C D B EF

10 Graphs: Representation One simple representation is the 2-dimensional array, known as adjacency matrix. [Programming Practicum, December 2009] A C D B EF ABCDEF A5 B113 C351 D E2 F4 (There are other graph representations that can give rise to faster algorithms. We introduce adjacency matrix for its simplicity.)

11 Graphs: Exploration How do you determine the in-degree and out- degree of each node by using the adjacency matrix representation of the directed graph? [Programming Practicum, December 2009] A C D B EF ABCDEF A5 B113 C351 D E2 F4

12 Matrix Multiplication (1/2) Multiplication on square matrices (2-dimensional arrays)  To compute C = A  B, where A, B, C are matrices c i,j = (a i,0  b 0,j ) + (a i,1  b 1,j ) (a i,n-1  b n-1,j ) c i,j is sum of terms produced by multiplying the elements of A’s row i with B’s column j. [Programming Practicum, December 2009]  =

13 Matrix Multiplication (2/2) In CS1101, you were given Matrices.java and told to complete the matrixProduct() method.  Download Matrices.java from the Practicum website. [Programming Practicum, December 2009]

14 THE END