The Aftercow Replacing math with cow since 1993. PotW Solution PotW Solution (credits to Jimmy) class Edge implements Comparable { int toNode, dist; public.

Slides:



Advertisements
Similar presentations
Inserting a Node into a Specified Position of a Linked List To create a node for the new item newNode = new Node(item); To insert a node between two nodes.
Advertisements

2012 = 2 2 * 503 Might be useful tomorrow. – MG. PotW Solution int dp(int pos, int k) { if (k > lim) return -(1
For(int i = 1; i
CS 206 Introduction to Computer Science II 04 / 01 / 2009 Instructor: Michael Eckmann.
Linked list Applications: Polynomial handling. Representing a polynomial using a linked list Store the coefficient and exponent of each term in nodes.
CSE 390B: Graph Algorithms Based on CSE 373 slides by Jessica Miller, Ruth Anderson 1.
Computer Science 209 Software Development Equality and Comparisons.
(())()(()) ((())()(())()(()))
This title is orange because of Halloween. It is totally not because it’s always orange.
I couldn’t think of anything to put here. So have a cow. Or two.
Chapter 15 Heaps. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Define a heap abstract data structure Demonstrate.
1 Graphs: Traversal Searching/Traversing a graph = visiting the vertices of a graph by following the edges in a systematic way Example: Given a highway.
Management Science 461 Lecture 2b – Shortest Paths September 16, 2008.
Semester 10 Time sure flies.. PotW Solution One possible solution is to randomly search the grid: o At each point in your search, look at the (up to four)
Multi-Source Shortest Paths T. Patrick Bailey CSC 5408 Graph Theory 4/28/2008.
CS 206 Introduction to Computer Science II 11 / 10 / 2008 Instructor: Michael Eckmann.
Greedy Algorithms Reading Material: Chapter 8 (Except Section 8.5)
Shortest Path Algorithm What is the Shortest Path Problem? Is the shortest path problem well defined? The Dijkstra's Algorithm for Shortest Path Problem.
> >
Example: HP ≤ p HC Hamiltonian Path –Input: Undirected Graph G = (V,E) –Y/N Question: Does G contain a Hamiltonian Path? Hamiltonian Cycle –Input: Undirected.
Greedy Algorithms Like dynamic programming algorithms, greedy algorithms are usually designed to solve optimization problems Unlike dynamic programming.
Dijkstra’s Algorithm Slide Courtesy: Uwash, UT 1.
The Travelling Salesman Algorithm A Salesman has to visit lots of different stores and return to the starting base On a graph this means visiting every.
CSE Walk through of the Dijkstra and A* algorithms in Python Joseph Xu.
1 Shortest Path Calculations in Graphs Prof. S. M. Lee Department of Computer Science.
Graphs CS 400/600 – Data Structures. Graphs2 Graphs  Used to represent all kinds of problems Networks and routing State diagrams Flow and capacity.
Course notes CS2606: Data Structures and Object-Oriented Development Graphs Department of Computer Science Virginia Tech Spring 2008 (The following notes.
UNCA CSCI November, 2001 These notes were prepared by the text’s author Clifford A. Shaffer Department of Computer Science Virginia Tech Copyright.
Graphs 2 Kevin Kauffman CS 309s. Problem We have sprinklers in our yard which need to be connected with pipe. Assuming pipes may only intersect at sprinkler.
ECE 643- Design and Analysis of Computer Networks K Shortest Paths Dept. of Electrical and Computer Eng. George Mason University Fairfax, VA ,
Dijkstra’s algorithm N: set of nodes for which shortest path already found Initialization: (Start with source node s) n N = {s}, D s = 0, “s is distance.
GRAPHS
ALG0183 Algorithms & Data Structures Lecture 21 d Dijkstra´s algorithm 8/25/20091 ALG0183 Algorithms & Data Structures by Dr Andy Brooks Chapter 14 Weiss.
COSC 2007 Data Structures II Chapter 14 Graphs III.
Prof. Swarat Chaudhuri COMP 482: Design and Analysis of Algorithms Spring 2012 Lecture 10.
Shortest Path Problem Weight of the graph –Nonnegative real number assigned to the edges connecting to vertices Weighted graphs –When a graph.
Computer Science Club It is easier to change the specification to fit the program than vice versa.
Shortest Path in Weighted Graph : Dijkstra’s Algorithm.
D IJKSTRA ' S ALGORITHM. S INGLE -S OURCE S HORTEST P ATH P ROBLEM Single-Source Shortest Path Problem - The problem of finding shortest paths from a.
1 Beginning & Intermediate Algebra – Math 103 Math, Statistics & Physics.
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,
Eulerian Paths and Cycles. What is a Eulerian Path Given an graph. Find a path which uses every edge exactly once. This path is called an Eulerian Path.
Contest Algorithms January 2016 Describe shortest path trees, SSSP, APSP, and three algorithms: Dijkstra, Bellman-Ford, Floyd-Warshall 9. Shortest Paths.
1 Beginning & Intermediate Algebra – Math 103 Math, Statistics & Physics.
Decision Maths 1 Shortest path algorithm Dijkstra’s Algorithm A V Ali :
Honors Track: Competitive Programming & Problem Solving Layered Graphs Technique Mark Leenen January 6 th.
7 Finding Bridge in a Graph. What is a bridge ? A C D B F G E.
Po-Lung Chen (Dont block me) d091: Urban Transport System 2010/03/26 (1) d091: Urban Transport System Po-Lung Chen Team Dont Block Me, National Taiwan.
1 3/21/2016 MATH 224 – Discrete Mathematics First we determine if a graph is connected.
Spanning Trees Dijkstra (Unit 10) SOL: DM.2 Classwork worksheet Homework (day 70) Worksheet Quiz next block.
Solving Inequalities Using Addition or Subtraction Honors Math – Grade 8.
Minimum Spanning Trees Kruskal's and Prim's algorithms.
Minimum Spanning Trees
Data Structures and Algorithms I Day 19, 11/3/11 Edge-Weighted Graphs
Lecture 23 CSE 331 Oct 26, 2016.
Graphs and Cycles.
Dijkstra Algorithm.
Minimum Spanning Tree Algorithms
Slide Courtesy: Uwash, UT
Advanced Computer Networks
Shortest Path Algorithm for Weighted Non-negative Undirected Graphs
Dijkstra’s Shortest Path Algorithm
Slide Courtesy: Uwash, UT
Minimum Spanning Trees
Dijkstra's Shortest Path Algorithm
Shortest Path Solutions
Line Graphs.
Notes on Minimum Spanning Tree problem
SEATS AFTER SALES SERVICES (direct & platform suppliers)
Presentation transcript:

The Aftercow Replacing math with cow since 1993

PotW Solution PotW Solution (credits to Jimmy) class Edge implements Comparable { int toNode, dist; public Edge(int a, int b) { toNode = a; dist = b; } public int compareTo(Edge e) { return dist - e.dist; } } Used for both storing the graph and locating closest points within the PriorityQueue in Dijkstra's

PotW Solution (cont.) for (int i = 0; i < e; i++) { int node1 = input.nextInt() - 1; int node2 = input.nextInt() - 1; int dist = input.nextInt(); graphThere[node1].add(new Edge(node2, dist)); graphBack[node2].add(new Edge(node1, dist)); } int[] distThere = dijkstra(graphThere, v, e); int[] distBack = dijkstra(graphBack, v, e); int count = 0; for (int i = 0; i < v; i++) { if (distThere[i] + distBack[i] <= money) { count++; } } System.out.println(count);

PotW Solution (cont.) int[] dijkstra(ArrayList [] graph, int v, int e) {... while (!pq.isEmpty()) { Edge e1 = pq.poll(); if (visited[e1.toNode]) continue; visited[e1.toNode] = true; for (int i = 0; i < graph[e1.toNode].size(); i++) { Edge e2 = graph[e1.toNode].get(i); int nextDist = dist[e1.toNode] + e2.dist; if (dist[e2.toNode] > nextDist) { pq.offer(new Edge(e2.toNode, nextDist)); dist[e2.toNode] = nextDist; } return dist; }

January Silver #1: Delivery Route N (1 <= N <= 100) farms located at different (integer) positions in the x-y plane Farmer John wants to visit each farm in sequential order and then return to farm 1 (1, 2, 3, …, N, 1) Takes one minute to make a step either north, south, east, or west You can only cross each farm once Determine the minimum amount of time it’ll take to complete entire delivery route (or -1 if no feasible delivery route is possible)

Delivery Route “Minimum time” suggests Dijkstra’s algorithm o Constructing the graph is the hard part Create 5 nodes for each farm, one for the farm itself and the 4 adjacent coordinates Add an edge between every pair of nodes such that it’s possible to go from (x1, y1) to (x2, y2) using a right-angle path w/o intersecting other farms o “Right-angle path” = path that changes direction at most once, moves only up/down/left/right o Runtime of O(N 3 ) works Use this graph w/ Dijkstra’s algorithm o Shortest path between farms is achievable using only these nodes and right-angle paths

January Gold #3: Bovine Alliance The cows in each of N farms (1 <= N <= 100,000) were instructed to build a trail to exactly one other farm (N trails total) o However, only M of these trails have been built Farms are arguing over which farms have built trails Calculate the number of ways the M trails could have been built, modulo 1,000,000,007.

Bovine Alliance Imagine it as a set of vertices and edges, with edges pointing to the vertex that built the edge o Count number of assignments such that each edge points to some vertex, and each vertex has at most one edge pointing to it Solve the problem for each connected component, multiply the number of possiblities for each together If component has n vertices, there are two different cases for the number of edges: o n-1: component is a tree, number of assignments is n You can leave any of the n cows, and there is one solution for each such possibility o n: component is a cycle, number of assignments is 2 The cycle can point in two directions Use Depth-First Search to find and categorize each connected component o Count node and edges as you DFS

December Gold/Silver #1: Cow Photography Farmer John wants to take a photograph of up to 20,000 cows standing in a specific order Every time he’s about to take a picture a group of zero or more cows moves to a set of new positions This happens for 5 photographs o Each cow actively moves in at most one photograph Given the contents of each photograph, reconstruct the original intended order of the cows o This sounds familiar…

Cow Photography The solution basically involves sorting the cows Consider two cows from the set, A and B Neither A nor B moved in at least 3 of the 5 photographs Compare the number of times A comes before B o 3 or more: A is before B in original ordering o Else: B is before A Use this as comparator and sort the list

PotW No Problem of the Week this week o Practice on your own! :D Submissions for the tiebreaker “Cow Trails” problem over the December break have been graded and ranked Will be announced next meeting, along with prizes for the top scorers of first semester!