1 / 24

CS 240: Data Structures

CS 240: Data Structures. Tuesday, July 22 nd Graphs, Applications of Previous Data Structures. Remaining Readings. See Assignment #5 You may elect to hand in Assignment #4 and/or Lab 6 for credit. If you don’t hand them in, they won’t count towards your grade. Otherwise, they will.

bdriskell
Download Presentation

CS 240: Data Structures

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. CS 240: Data Structures Tuesday, July 22nd Graphs, Applications of Previous Data Structures

  2. Remaining Readings • See Assignment #5 • You may elect to hand in Assignment #4 and/or Lab 6 for credit. If you don’t hand them in, they won’t count towards your grade. Otherwise, they will. • They need to be handed in on Wednesday.

  3. Representation • Now, our Node needs new data: • A list of Node* instead of just “next” • Some way to select a “next” • Graphs will often take distance between Nodes into account (so far, our distances have been irrelevant) • Hence each Node* is associated with a distance • We can store this as a “pair<Node*,int>” • Requires #include<algorithm>

  4. Linked List • A Linked List is a subset of Graph. • It has nodes with only 1 Node* (list size == 1) • And the distance between each Node is the same (no value is needed, but we might as well say 0).

  5. Binary Trees • A Binary Tree is a subset of graph • Each node has 2 Node* • A node in a tree always points to a node closer to the bottom of the tree. There are no cycles!

  6. N Trees • Just like binary trees, but up to N Node*

  7. So, what is a graph? • A graph is a set of Nodes with N Node*. • However, the Node* has no limitations. • Node pointers may be associated with a distance. • Cycles could occur!

  8. Node: • Our node needs: • T data; • A list of Node<T>* • A list of associated distances

  9. Representations • Aside from a Node based representation, we have other ways to represent a graph:

  10. Alternate Representations • Adjacency Matrix • What if the graph is small?

  11. Alternate Representations • Adjacency List

  12. Hashing • A hash can be represented with a graph. • Each index -> the storage for that index

  13. Terms • Out-Degree • In-Degree • Cycle • Directed • Undirected

  14. Insertion • To insert into a graph: • We need to know where the node is going. • Who points to it? • Who does it point to? • What are the associated distances?

  15. Removal • Removal requires us to update every node that points to us. • With an undirected graph, this is easy. • On a directed graph, we don’t know who points to a particular node!

  16. Copying • Copying Nodes is no longer trivial. • Generally, a graph will maintain or create an adjacency matrix/list in order to transfer the appropriate information. • We could travel each of the nodes and copy them one at a time. However, linking them together in this manner is arduous. Or, it they aren’t connected…. Hmm • If it is possible that all the nodes aren’t connected then we need to keep more information about our nodes.

  17. First • A graph may no longer has a concept of a “first” node. • It may be reasonable to be able to start anywhere. However, that is not always the case. • Therefore, we need to be able to travel the graph.

  18. Destruction • We need to find all of the nodes and delete them. • The question is: How do we find them all?

  19. Searching • Searching and traversing are more difficult because of cycles.

  20. Depth-first • Starting at “first” • At a node: • Select the next destination and go it to (if you haven’t been there already). • Why does this work? Can you code this? • This uses “backtracking”.

  21. Breadth-First • Starting at “first” • At a node: • Place all destinations in a queue (if you haven’t been to them before). • Dequeue and go to that destination (if you haven’t been to that location) until empty • What happens if you apply this to a binary tree?

  22. Trees • Trees are a type of directed, acyclic graph. • A node (a parent, who can also be a child) has some number of children. • Nodes without children are called leaf nodes. • A parent, who is not also a child, is called the root node.

  23. Tree • Without some sort of organization, trees aren’t terribly useful. • How do we intend to use the tree? • The most common type of specialized tree is a binary search tree (BST). • Rules: • A node has up to 2 children (left and right) • The left child is < the parent • The right child is >= the parent

  24. Trees • Every node within a Tree is a Tree onto itself. Therefore, we can design all of our code in a recursive fashion. • How do we? • Insert? • Remove? • Search? • Empty? • Traverse? (Three traversals)

More Related