CS 240: Data Structures Tuesday, July 31 st Graphs, Applications of Previous Data Structures.

Post on 19-Dec-2015

218 views 0 download

transcript

CS 240: Data StructuresCS 240: Data Structures

Tuesday, July 31Tuesday, July 31stst

Graphs, Applications of Previous Graphs, Applications of Previous Data StructuresData Structures

Remaining ReadingsRemaining Readings Chapter 7-7.3 (up to p.426)Chapter 7-7.3 (up to p.426) P. 435 on backtrackingP. 435 on backtracking Chap 8-8.5 (up to p.484)Chap 8-8.5 (up to p.484) Section 8.6 (p.496-505)Section 8.6 (p.496-505) Chapter 11-11.3 (up to p.643)Chapter 11-11.3 (up to p.643) Section 11.4,11.5 (p.656-681)Section 11.4,11.5 (p.656-681)

This portion is a bit excessive, only look at what you This portion is a bit excessive, only look at what you need to knowneed to know

Chapter 12-12.2 (up to p.697)Chapter 12-12.2 (up to p.697) Section 12.3 (p.701-703)Section 12.3 (p.701-703) Section 12.4 (p.715-727)Section 12.4 (p.715-727)

Start

RepresentationRepresentation

Now, our Node needs new data:Now, our Node needs new data: A list of Node* instead of just “next”A list of Node* instead of just “next”

Some way to select a “next”Some way to select a “next”

Graphs will often take distance between Nodes Graphs will often take distance between Nodes into account (so far, our distances have been into account (so far, our distances have been irrelevant)irrelevant) Hence each Node* is associated with a distanceHence each Node* is associated with a distance

We can store this as a “pair<Node*,int>”We can store this as a “pair<Node*,int>” Requires #include<algorithm>Requires #include<algorithm>

Linked ListLinked List

A Linked List is a subset of Graph.A Linked List is a subset of Graph. It has nodes with only 1 Node* (list size == It has nodes with only 1 Node* (list size ==

1)1)And the distance between each Node is And the distance between each Node is

the same (no value is needed, but we the same (no value is needed, but we might as well say 0).might as well say 0).

Binary TreesBinary Trees

A Binary Tree is a subset of graphA Binary Tree is a subset of graphEach node has 2 Node*Each node has 2 Node*A node in a tree always points to a node A node in a tree always points to a node

closer to the bottom of the tree. There are closer to the bottom of the tree. There are no cycles!no cycles!

N TreesN Trees

Just like binary trees, but up to N Node*Just like binary trees, but up to N Node*

So, what is a graph?So, what is a graph?

A graph is a set of Nodes with N Node*.A graph is a set of Nodes with N Node*.However, the Node* has no limitations.However, the Node* has no limitations.Node pointers may be associated with a Node pointers may be associated with a

distance.distance.Cycles could occur!Cycles could occur!

Node:Node:

Our node needs:Our node needs:

T data;T data;A list of Node<T>*A list of Node<T>*A list of associated distancesA list of associated distances

RepresentationsRepresentations

Let’s look at locations as a representation Let’s look at locations as a representation of a graph.of a graph.

Alternate RepresentationsAlternate Representations

Adjacency MatrixAdjacency MatrixWhat if the graph is small?What if the graph is small?

Alternate RepresentationsAlternate Representations

Adjacency ListAdjacency List

HashingHashing

A hash can be represented with a graph.A hash can be represented with a graph.

TermsTerms

Out-DegreeOut-Degree In-DegreeIn-DegreeCycleCycleDirectedDirectedUndirectedUndirected

InsertionInsertion

To insert into a graph:To insert into a graph:We need to know where the node is going.We need to know where the node is going.

Who points to it?Who points to it?Who does it point to?Who does it point to?What are the associated distances?What are the associated distances?

RemovalRemoval

Removal requires us to update every node Removal requires us to update every node that points to us.that points to us.

With an undirected graph, this is easy.With an undirected graph, this is easy.On a directed graph, we don’t know who On a directed graph, we don’t know who

points to a particular node!points to a particular node!

CopyingCopying

Copying Nodes is no longer trivial.Copying Nodes is no longer trivial.Generally, a graph will maintain or create Generally, a graph will maintain or create

an adjacency matrix/list in order to transfer an adjacency matrix/list in order to transfer the appropriate information.the appropriate information.

We could travel each of the nodes and We could travel each of the nodes and copy them one at a time. However, linking copy them one at a time. However, linking them together in this manner is arduous.them together in this manner is arduous.

FirstFirst

A graph may no longer has a concept of a A graph may no longer has a concept of a “first” node.“first” node.

It may be reasonable to be able to start It may be reasonable to be able to start anywhere. However, that is not always the anywhere. However, that is not always the case.case.

Therefore, we need to be able to travel the Therefore, we need to be able to travel the graph.graph.

DestructionDestruction

Start at some node (X), find some Start at some node (X), find some destination (Y). Graph::remove(X), repeat destination (Y). Graph::remove(X), repeat this at Y (X=Y, start over) unless Y is this at Y (X=Y, start over) unless Y is NULL.NULL.

SearchingSearching

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

Depth-firstDepth-first

Starting at “first”Starting at “first”At a node:At a node:

Select the next destination and go it to (if you Select the next destination and go it to (if you haven’t been there already).haven’t been there already).

Why does this work? Can you code this?Why does this work? Can you code this?This uses “backtracking”.This uses “backtracking”.

Breadth-FirstBreadth-First

Starting at “first”Starting at “first” At a node:At a node:

Place all destinations in a queue (if you Place all destinations in a queue (if you haven’t been to them before).haven’t been to them before).

Dequeue and go to that destination (if you Dequeue and go to that destination (if you haven’t been to that location) until emptyhaven’t been to that location) until empty

What happens if you apply this to a binary What happens if you apply this to a binary tree?tree?

Applications of ADTsApplications of ADTs

How do we create the following?How do we create the following?A schedule of tasksA schedule of tasksA schedule of tasks with priority (like an A schedule of tasks with priority (like an

OS)OS)An activation record for recursive An activation record for recursive

functions/methodsfunctions/methods

Questions?Questions?

… … there is still more, but I’m hinting that there is still more, but I’m hinting that you should ask questions now…you should ask questions now…

Hint, hint.Hint, hint.

A challenger appears!A challenger appears!

Prepare for a quiz!Prepare for a quiz!