1 / 23

CSE 121/131 Programming Spring 2001 Handout 9

CSE 121/131 Programming Spring 2001 Handout 9. Graphs. 1. 3. 2. 5. 4. Perhaps the most important object considered in CS. What is a graph? (Informal). Red circles denote nodes or vertices or points. Blue lines denote edges . Edges connect pairs of nodes.

yan
Télécharger la présentation

CSE 121/131 Programming Spring 2001 Handout 9

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSE 121/131ProgrammingSpring 2001 Handout 9

  2. Graphs 1 3 2 5 4 Perhaps the most important object considered in CS. What is a graph? (Informal) Red circles denote nodes or vertices or points. Blue lines denote edges. Edges connect pairs of nodes. This graph has vertex set: {1,2,3,4,5} Has edge set: {(1,2), (2,5), (5,4), (1,5)}.

  3. Graphs and puzzles B A The Bridges of Konigsburg Park R. Pregel D B C A D Question: On a walk through the park can you walk over each of the bridges exactly once? C

  4. More Puzzles... Which of these figures can you draw w/o retracing steps or lifting pencil off paper? Euler, a famous Swiss mathematician solved all of these problems and initiated the field of graph theory in the process. A walk through a graph that goes through every edge exactly once (and returns) is called an Eulerian tour.

  5. Knight’s moves b8 c6 1 2 3 4 5 6 7 8 c5 a6 c7 a8 a b c d e f g h Question: Can a knight make a tour of the entire chessboard? Question about graphs: Is there a tour of the graph that visits all the vertices exactly once? (Hamiltonian cycle.) b6

  6. Traveling Salesperson problem (TSP) Seattle Minneapolis Chicago San Francisco Boston $119 New York Philadelphia Denver Atlanta Dallas Los Angeles Miami Given (one-way) airfare between each pair of cities, find least cost Hamiltonian tour.

  7. Radio station frequencies Nodes are radio stations. Edges show stations that interfere with each other. Assign one of four frequencies to each of radio stations so that interfering stations get different frequencies.

  8. Adjacency list representation 1 3 2 0 4 Won’t work to have each node have a fixed set of fields as for binary trees… Instead, can make each node have a list of nodes it is joined to. Adjacency list representation. 1 4 2 0 2 1 0 0

  9. Adjacency matrix representation 1 3 2 0 4 At position (i,j) a 1 indicates an edge between nodes i and j and a 0 indicates no edge.

  10. Graph variations Graphs we have seen --- undirected graphs. When edges have directions --- directed graphs. (One-way streets in a city.) When edges have costs/distances on them --- weighted graphs. Trees are special kinds of graphs. What kind?

  11. Basic definitions 1 3 2 0 4 Graph G = (V,E). V is a set of vertices. E is a set of pairs of vertices. 1 and 2 are adjacent. 1,2,0,4 is a path. 1,2,0 is a cycle. Graph is not connected since there is no way to get to 3 from 1. {0,1,2,4} is a connected component. So is {3}. A tree is a connected graph without cycles.

  12. Binary Trees We saw how binary trees were useful for implementing priority queues. However, they arise in a number of other contexts as well. Heaps are rather special because they satisfy the structure property. Because of this, it is possible to implement them as arrays. For other applications it is impossible or very expensive to maintain the structure property. We then have to implement binary trees as linked structures. One of the most basic things we want to do with a binary tree is to traverse it, i.e., to systematically visit all its nodes.

  13. Binary Tree Traversals • There are many ways of traversing a binary tree. • Three of the most popular ones are • Postorder: left, right, root. • Inorder: left, root, right. • Preorder: root, left, right. We already saw that for expression trees: inorder traversals give infix expressions. postorder traversals give postfix expressions. Traversals arise in a lot of other situations as well. Since a binary tree is put together recursively from smaller trees, recursive algorithms are natural here. Alternatively we can use a stack data structure.

  14. Iterator for postorder traversal Each iterator maintains a stack explicitly to keep track of the state. In stack, keep track of nodes and number of times they have been popped from stack. If a node is popped for the first time, push it back, increment “pop-counter” and visit left side. If popped for the second time, push it back, increment “pop-counter” and visit right side. If popped for the third time, visit node itself.

  15. Applications of Tree Traversals Computing heights and depths of nodes. depth(v) = depth(w) + 1 ht(v) = max(ht(x), ht(y)) + 1 Use appropriate traversals to compute. w v y x

  16. Graph traversal Assume graph is given by adjacency list. • Breadth-first search: • Start at some node, s. Put s in queue Q. • While Q is non-empty • dequeue a node, • mark it, • put its unmarked neighbors in Q. Applications?

  17. What was this course about? • Problem solving with software • Correctness of software • Efficiency of software • What is problem solving? Analyzing the task, designing and refining a software solution. • What is correctness? Software is designed with a goal in mind. Does it meet this goal? • How do we choose between different correct solutions? Important criterion --- Efficiency

  18. Correctness • Specification: captures the requirements (“logic”) • Implementation: realizes a spec (“computation”) • Conceptual tools (divide to conquer!): • Abstraction levels (implementations in terms of more basic specifications) • Modularization (horizontal and vertical)

  19. Efficiency • Determined • empirically (measurements) • analytically (eg. running time analysis) complexity • Conceptual tools for complexity • Models of computation • Asymptotic comparisons • Worst-case • Amortized

  20. Data structures • Abstract data types • Provides modularity for correctness • Critical efficiency (since they are used repeatedly) • We covered: • Lists (mutable and immutable) • Stacks • Queues • Ranked sequences • Priority queues • Binary heaps • Dictionaries • Search trees • Hash tables

  21. Techniques (Tricks of the trade…) • Linked lists, double-linked lists, end “pointer” • Array doubling • Circular arrays • Balancing binary trees (AVL) • Binary heaps as arrays • ETC.!

  22. Java • An imperative language (most computation is in sequences of statements) • An object-oriented language (almost everything is an object!) • Small core language • Practical usefulness through JDK (Java Development Kit) classes etc. • Exploits particularly well the concepts of interface, abstract class, inheritance (with much method overriding) • GUIs • Applets

  23. Elements of software engineering • Making use of the JDK. • Specification, abstraction, modularization • Design patterns • Threads • We didn’t talk about these, but they are important (check out CSE 350): • Tools for software design, eg. UML (Unified Modeling Language) • Software life cycle methodologies, software process models. • Verification, testing, formal methods. • COTS vs. SE standards vs. open source

More Related