CS 106X, Lecture 21 Tries; Graphs
Total Page:16
File Type:pdf, Size:1020Kb
CS 106X, Lecture 21 Tries; Graphs reading: Programming Abstractions in C++, Chapter 18 This document is copyright (C) Stanford Computer Science and Nick Troccoli, licensed under Creative Commons Attribution 2.5 License. All rights reserved. Based on slides created by Keith Schwarz, Julie Zelenski, Jerry Cain, Eric Roberts, Mehran Sahami, Stuart Reges, Cynthia Lee, Marty Stepp, Ashley Taylor and others. Plan For Today • Tries • Announcements • Graphs • Implementing a Graph • Representing Data with Graphs 2 Plan For Today • Tries • Announcements • Graphs • Implementing a Graph • Representing Data with Graphs 3 The Lexicon • Lexicons are good for storing words – contains – containsPrefix – add 4 The Lexicon root the art we all arts you artsy 5 The Lexicon root the contains? art we containsPrefix? add? all arts you artsy 6 The Lexicon S T A R T L I N G S T A R T 7 The Lexicon • We want to model a set of words as a tree of some kind • The tree should be sorted in some way for efficient lookup • The tree should take advantage of words containing each other to save space and time 8 Tries trie ("try"): A tree structure optimized for "prefix" searches struct TrieNode { bool isWord; TrieNode* children[26]; // depends on the alphabet }; 9 Tries isWord: false a b c d e … “” isWord: true a b c d e … “a” isWord: false a b c d e … “ac” isWord: true a b c d e … “ace” 10 Reading Words Yellow = word in the trie A E H S / A E H S A E H S A E H S / / / / / / / / A E H S A E H S A E H S A E H S / / / / / / / / / / / / A E H S A E H S A E H S / / / / / / / / / / A E H S A E H S / / / / / / / / 11 Reading Words A E H S / A E H S A E H S A E H S / / / / / / / / A E H S A E H S A E H S A E H S / / / / / / / / / / / / A E H S A E H S A E H S / / / / / / / / / / A E H S A E H S / / / / / / / / 12 Reading Words A E H S / A E H S A E H S A E H S / / / / / / / / A E H S A E H S A E H S A E H S / / / / / / / / / / / / A E H S A E H S A E H S / / / / / / / / / / A E H S A E H S / / / / / / / / h 13 Reading Words A E H S / A E H S A E H S A E H S / / / / / / / / A E H S A E H S A E H S A E H S / / / / / / / / / / / / A E H S A E H S A E H S / / / / / / / / / / A E H S A E H S / / / / / / / / h a 14 Reading Words A E H S / A E H S A E H S A E H S / / / / / / / / A E H S A E H S A E H S A E H S / / / / / / / / / / / / A E H S A E H S A E H S / / / / / / / / / / A E H S A E H S / / / / / / / / h a s 15 Reading Words A E H S / A E H S A E H S A E H S / / / / / / / / A E H S A E H S A E H S A E H S / / / / / / / / / / / / A E H S A E H S A E H S / / / / / / / / / / A E H S A E H S / / / / / / / / h a s h 16 Reading Words A E H S / A E H S A E H S A E H S / / / / / / / / A E H S A E H S A E H S A E H S / / / / / / / / / / / / A E H S A E H S A E H S / / / / / / / / / / A E H S A E H S / / / / / / / / h a s h 17 Reading Words What are all the A E H S / words represented by this trie? A E H S A E H S A E H S / / / / / / / / A E H S A E H S A E H S A E H S / / / / / / / / / / / / A E H S A E H S A E H S / / / / / / / / / / A E H S A E H S / / / / / / / / 18 Reading Words What are all the A E H S / words represented by this trie? A E H S A E H S A E H S / / / / / / / / a as A E H S A E H S A E H S A E H S / / / / / / / / / / / / ha haha A E H S A E H S A E H S he / / / / / / / / / / she A E H S A E H S / / / / / / / / 19 Reading Words What are all the A E H S / words represented by this trie? A E H S A E H S A E H S / / / / / / / / a as A E H S A E H S A E H S A E H S / / / / / / / / / / / / ha haha A E H S A E H S A E H S he / / / / / / / / / / she A E H S A E H S How can we write a / / / / / / / / function to print this? 20 PrintAllWords void printAllWords(TrieNode* root) { printAllWordsHelper(root, ""); } void printAllWordsHelper(TrieNode* root, string prefix) { if (root == nullptr) { return; } if (root->isWord) { cout << prefix << endl; } for (int i = 0; i < 26; i++) { printAllWordsHelper(root->children[i], prefix + char('a' + i)); } } 21 Prefixes How can we write A E H S / containsPrefix? A E H S A E H S A E H S containsPrefix(“a”) => true / / / / / / / / containsPrefix(“se”) => false A E H S A E H S A E H S A E H S / / / / / / / / / / / / A E H S A E H S A E H S / / / / / / / / / / A E H S A E H S / / / / / / / / 22 containsPrefix bool containsPrefix(TrieNode* node, string prefix) { if (node == nullptr) { return false; } if (prefix.length() == 0) { return true; } return containsPrefix(node->children[prefix[0] - 'a'], prefix.substr(1)); } 23 Plan For Today • Tries • Announcements • Graphs • Implementing a Graph • Representing Data with Graphs 24 Announcements • Brown Institute Data Visualization @ NY Times talk with Kevin Quealy Tues. 11/13 4:30PM in Packard 101 25 Plan For Today • Tries • Announcements • Graphs • Implementing a Graph • Representing Data with Graphs 26 Linked Structures 2 5 8 27 Linked Structures 2 5 8 28 Linked Structures 2 1 3 What about What about What about storing bidirectional links? representing cycles? data in the links, like distances or costs? 24 29 Linked Structures: Shortfalls Representing airline flights and distances: 849 PVD 1843 ORD SFO 142 337 802 LGA 1743 2555 1387 1099 HNL 1233 LAX DFW 1120 MIA 30 Linked Structures: Shortfalls 31 Linked Structures: Shortfalls 32 Linked Structures: Shortfalls 33 Graphs A graph consists of a set of nodes connected by edges. Graphs can model: - Sites and links on the web - Disease outbreaks - Social networks - Geographies - Task and dependency graphs - and more… 34 Graphs A graph consists of a set of nodes connected by edges. 35 Nodes Nodes (“Vertices”) 36 Edges Edges (“Arcs”) Directed 37 Edges Edges (“Arcs”) Undirected 38 Edges Edges (“Arcs”) Unweighted 39 Weights A weight is a cost associated with a given edge. Edges (“Arcs”) 12 Weighted 5 6 1 18 4 12 22 6 40 Degree Degree (# edges touching a node) 5 3 2 6 1 1 41 Degree In-degree (# edges coming into a node) 2 1 2 2 1 1 42 Degree Out-degree (# edges leaving a node) 3 2 0 4 0 0 43 Degree Degree = In-degree + Out-degree 5 3 2 6 1 1 44 Neighbors Neighbors (nodes you can directly reach) 45 Neighbors Neighbors (nodes you can directly reach) 46 Neighbors Neighbors (nodes you can directly reach) 47 Neighbors Neighbors (nodes you can directly reach) 48 Neighbors Neighbors (nodes you can directly reach) 49 Paths Path (sequence of Paths are edges that go from represented as nodes one node to another) B visited or edges taken A Path length = 3 C (number of D nodes or edges in the path) F E 50 Paths Path (sequence of edges that go from one node to another) B A C D Node x is reachable from node y if a path F exists from y to E x. 51 Cycles Cycle (path that begins and ends at the same node) B A C D F E 52 Loops Loop (an edge that connects a node to itself) B A C D F E 53 Graph Properties A graph is connected if every node is reachable from every other node. 54 Graph Properties A graph is complete if every node has a direct edge to every other node. 55 Graph Properties A graph is acyclic if it does not contain any cycles. 56 Graph Properties A graph is weighted if its edges have weights, or unweighted if its edges do not have weights. 6 12 2 5 14 weighted unweighted 57 Graph Properties A graph is directed if its edges have direction, or undirected if its edges do not have direction (aka are bidirectional). directed undirected 58 Graph Properties • Connected or unconnected • Acyclic • Directed or undirected • Weighted or unweighted • Complete Which of these properties do binary trees have? What about linked lists? 59 One Big, Happy Family • A binary tree is a graph with some restrictions: – The tree is an unweighted, directed, acyclic graph (DAG). – Each node's in-degree is at most 1, and out-degree is at most 2. – There is exactly one path from the root to every node. F • A linked list is also a graph: – Unweighted DAG. – In/out degree of at most 1 for all nodes.