240 likes | 264 Views
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.
E N D
CS 240: Data Structures Tuesday, July 22nd 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. • They need to be handed in on Wednesday.
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>
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).
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!
N Trees • Just like binary trees, but up to N Node*
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!
Node: • Our node needs: • T data; • A list of Node<T>* • A list of associated distances
Representations • Aside from a Node based representation, we have other ways to represent a graph:
Alternate Representations • Adjacency Matrix • What if the graph is small?
Alternate Representations • Adjacency List
Hashing • A hash can be represented with a graph. • Each index -> the storage for that index
Terms • Out-Degree • In-Degree • Cycle • Directed • Undirected
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?
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!
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.
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.
Destruction • We need to find all of the nodes and delete them. • The question is: How do we find them all?
Searching • Searching and traversing are more difficult because of cycles.
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”.
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?
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.
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
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)