lecture 210 ct 4

51
Graphs Dr. Diana Hintea Lecturer in Computer Science [email protected] 210CT Week 7 13.11.2015

Upload: tanasiev-stefy

Post on 28-Jan-2016

12 views

Category:

Documents


0 download

DESCRIPTION

Lecture 2014

TRANSCRIPT

Page 1: Lecture 210 Ct 4

Graphs

Dr. Diana Hintea

Lecturer in Computer Science

[email protected]

210CT Week 7

13.11.2015

Page 2: Lecture 210 Ct 4

Graphs Intro

Learning Outcomes

13.11.2015 1

Data Fusion between a 2D Laser Profile Sensor and a Camera

Graph Traversal (DFS)

Dijkstra’s Algorithm

Representation and Implementation

Graph Traversal (BFS)

Page 3: Lecture 210 Ct 4

Graphs

Data Fusion between a 2D Laser Profile Sensor and a Camera

13.11.2015 2

What are graphs?

• Set of nodes and connections between them.

Page 4: Lecture 210 Ct 4

Graphs

Data Fusion between a 2D Laser Profile Sensor and a Camera

13.11.2015 3

What are they used for?

Page 5: Lecture 210 Ct 4

Graphs

Data Fusion between a 2D Laser Profile Sensor and a Camera

13.11.2015 4

What are they used for?

• Map analysis, route finding, path planning • Ranking search results • Analysing related data, such as social networks, proteins, etc. • Compiler optimisation • Constraint satisfaction (timetabling) • Physics simulations (actual physics) • Physics simulations (games) • Social connections: Facebook uses graphs for this

Page 6: Lecture 210 Ct 4

Graphs

Data Fusion between a 2D Laser Profile Sensor and a Camera

13.11.2015 5

Facebook!

Page 7: Lecture 210 Ct 4

Graphs

Data Fusion between a 2D Laser Profile Sensor and a Camera

13.11.2015 6

Types of graphs

• Undirected graph: A link is both ways.

Page 8: Lecture 210 Ct 4

Graphs

Data Fusion between a 2D Laser Profile Sensor and a Camera

13.11.2015 7

Types of graphs

• Directed graph: Each link is directional and does not imply the inverse.

Page 9: Lecture 210 Ct 4

Graphs

Data Fusion between a 2D Laser Profile Sensor and a Camera

13.11.2015 8

Types of graphs

• Vertex labelled graph: The data used to identify each node is not the only data that is important about that node.

• For example, it might also have a "colour" value that affects algorithms.

Page 10: Lecture 210 Ct 4

Graphs

Data Fusion between a 2D Laser Profile Sensor and a Camera

13.11.2015 9

Types of graphs

• Cyclic graphs: Has at least one "cycle". That is, a path from a node that leads back to itself.

Page 11: Lecture 210 Ct 4

Graphs

Data Fusion between a 2D Laser Profile Sensor and a Camera

13.11.2015 10

Types of graphs

• Edge labelled graphs: • Links have data

values that might affect algorithms.

• Imagine in a graph of cities in which the links represent roads, the length of the road might be recorded as the edge data.

Page 12: Lecture 210 Ct 4

Graphs

Data Fusion between a 2D Laser Profile Sensor and a Camera

13.11.2015 11

Types of graph

• Weighted graph: The parameters along edges or at nodes are interval data and can be summed and compared.

Page 13: Lecture 210 Ct 4

Graphs

Data Fusion between a 2D Laser Profile Sensor and a Camera

13.11.2015 12

Types of graphs

• Directed acyclic graph: Links have direction. • No cycles exist. • Used a lot. Called a "DAG" for short. Possibly humorous to

Australians.

Page 14: Lecture 210 Ct 4

Graphs

Data Fusion between a 2D Laser Profile Sensor and a Camera

13.11.2015 13

Terminology

• The nodes are called vertices. • The links are called edges (sometimes arcs). • An edge is incident to/on a vertex if it connects it to another. • Connected vertices are called adjacent or neighbours. • A vertex's degree is the number of edges incident on it.

Page 15: Lecture 210 Ct 4

Graphs

Data Fusion between a 2D Laser Profile Sensor and a Camera

13.11.2015 14

Page 16: Lecture 210 Ct 4

Graphs

Data Fusion between a 2D Laser Profile Sensor and a Camera

13.11.2015 15

Vertex Degree (Undirected Graph)

Page 17: Lecture 210 Ct 4

Graphs

Data Fusion between a 2D Laser Profile Sensor and a Camera

13.11.2015 16

Vertex Degree (Directed Graph)

Page 18: Lecture 210 Ct 4

Representation and Implementation

Data Fusion between a 2D Laser Profile Sensor and a Camera

13.11.2015 17

Representing graphs

• We can represent vertices with integers (actually, any unique value, would do for most of the times).

• Edges are pairs of vertices, (a,b), connecting a and b. • A graph, G, has a set of vertices, V, and a set of edges, E. • We say G=(V,E). • We use n and m to represent the numbers of vertices and edges. • Think n for node if you find it hard to remember which way

around these are.

Page 19: Lecture 210 Ct 4

Representation and Implementation

Data Fusion between a 2D Laser Profile Sensor and a Camera

13.11.2015 18

Visualisation

• A graph G=(V,E) • Where:

• V={1,2,3,4,5} • E={{1,2},{2,4},{3,4},{3,5},{4,5}}

Page 20: Lecture 210 Ct 4

Representation and Implementation

Data Fusion between a 2D Laser Profile Sensor and a Camera

13.11.2015 19

Implementing Graphs

• There are two ways of implementing a graph when programming. • Adjacency Matrix:

• A two dimensional matrix of boolean values where the value of a cell i, j is true if vertices i and j are connected.

• Adjacency List: • Each vertex contains a list of vertices that it is connected to.

Page 21: Lecture 210 Ct 4

Representation and Implementation

Data Fusion between a 2D Laser Profile Sensor and a Camera

13.11.2015 20

Adjacency Matrix

• Note reflection around i=j, due to undirectedness. • Implied False in empty cells.

Page 22: Lecture 210 Ct 4

Representation and Implementation

Data Fusion between a 2D Laser Profile Sensor and a Camera

13.11.2015 21

Adjacency List

Page 23: Lecture 210 Ct 4

Representation and Implementation

Data Fusion between a 2D Laser Profile Sensor and a Camera

13.11.2015 22

Space complexity

• So far, the data structures we have looked at have all taken about the same amount of memory space 𝑂(𝑛), where n is the number of elements. • The adjacency matrix requires 𝑂(𝑛2) space, where n is the number of vertices. • Adjacency lists require 𝑂(𝑚) space where m is the number of edges. • This method uses less space (generally - what if every node is connected to every other?) and is faster to search for common operations.

Page 24: Lecture 210 Ct 4

Representation and Implementation

Data Fusion between a 2D Laser Profile Sensor and a Camera

13.11.2015 23

Weighted Graphs

• Sometimes it’s useful to store a number with each edge. • This changes the way graphs are represented. • The adjacency matrix is now numerical instead of boolean. • Unconnected nodes can be given a default value, such as infinity for shortest path finding. • The adjacency list must store the edges as pairs including the connection and the weight.

Page 25: Lecture 210 Ct 4

Representation and Implementation

Data Fusion between a 2D Laser Profile Sensor and a Camera

13.11.2015 24

Weighted Graph - Example

Page 26: Lecture 210 Ct 4

Representation and Implementation

Data Fusion between a 2D Laser Profile Sensor and a Camera

13.11.2015 25

Adjacency Matrix

Page 27: Lecture 210 Ct 4

Representation and Implementation

Data Fusion between a 2D Laser Profile Sensor and a Camera

13.11.2015 26

Adjacency List

Page 28: Lecture 210 Ct 4

Data Fusion between a 2D Laser Profile Sensor and a Camera

13.11.2015 27

Pre-Homework

• C++ people: create a simple object type (doesn't matter what) and then make an array of it. Try adding items to the array. Typically, we set all values to NULL and then we can check to see if each element of an array has been used by looking for NULL. • Python people: create a simple object type (doesn't matter what) and then make a list. How do you add and remove new objects from the list? How do you know how many items are in the list? How can you find a specific item from the list?

Page 29: Lecture 210 Ct 4

Data Fusion between a 2D Laser Profile Sensor and a Camera

13.11.2015 28

Homework

1. Write the pseudocode for an unweighted graph data structure. You either use an adjacency matrix or an adjacency list approach. Also write a function to add a new node and a function to add an edge. 2. Implement the graph you have designed in the programming language of your choice. You may use your own linked list from previous labs, the STL LL, or use a simple fixed size array (10 elements would be fine). 3. What changes would you need to make to include weighted vertices?

Page 30: Lecture 210 Ct 4

Data Fusion between a 2D Laser Profile Sensor and a Camera

13.11.2015 29

Homework

CLASS VERTEX label←∅ edges←[] v←new VERTEX v.label←24 v.edges.add(3)

Page 31: Lecture 210 Ct 4

Data Fusion between a 2D Laser Profile Sensor and a Camera

13.11.2015 30

Advanced Homework

• You must first complete the standard homework for this task. Look at the example code given previously for trees. You will find functions that output in the "dot" language of graphviz. This was used in lectures to show graphical representations of trees. Graphviz can also be used to display graphs. The examples given in this lecture are generated this way. Write a function that exports a graph as a dot file, or displays dot language output that can be redirected to a file.

Page 32: Lecture 210 Ct 4

13.11.2015 31

Data Fusion between a 2D Laser Profile Sensor and a Camera

BREAK!!!

Page 33: Lecture 210 Ct 4

Graph Traversal (DFS)

13.11.2015 32

Data Fusion between a 2D Laser Profile Sensor and a Camera

Just a nice video

Page 34: Lecture 210 Ct 4

13.11.2015 33

Data Fusion between a 2D Laser Profile Sensor and a Camera

• Storing data in a graph is fairly easy. • They don't really become useful until we can search for data or

find information about the interconnections. • Algorithms that interrogate the graph by following the edges are

called traversal algorithms.

Graph Traversal

Graph Traversal (DFS)

Page 35: Lecture 210 Ct 4

Graph Traversal (DFS)

13.11.2015 34

Data Fusion between a 2D Laser Profile Sensor and a Camera

• Visit all nodes beginning with v in graph G. • Sometimes called Depth First Traversal (DFT). • depthFirstSearch(G,v)

• Not searching for v, even though this is what it reads like…

Depth-First Search (DFS)

Page 36: Lecture 210 Ct 4

Graph Traversal (DFS)

13.11.2015 35

Data Fusion between a 2D Laser Profile Sensor and a Camera

• Traverses a graph by visiting each vertex in turn. • Finds the first edge for the current vertex. • Follows it to reach the next unvisited vertex. • Marks the vertex as visited. • Repeats until all vertices are marked as visited.

Depth-First Search (DFS)

Page 37: Lecture 210 Ct 4

Graph Traversal (DFS)

13.11.2015 36

Data Fusion between a 2D Laser Profile Sensor and a Camera

• https://www.cs.usfca.edu/~galles/visualization/DFS.html • http://visualgo.net/dfsbfs.html • https://www.youtube.com/watch?v=4AsEBQSjNUE • https://www.youtube.com/watch?v=zLZhSSXAwxI

DFS Illustration

Page 38: Lecture 210 Ct 4

Graph Traversal (DFS)

13.11.2015 37

Data Fusion between a 2D Laser Profile Sensor and a Camera

DEPTH-FIRST-SEARCH (G, v) S←new Stack() visited←[] S.push(v) while S is not empty u←S.pop() if u is not in visited visited.append(u) for all edges, e, from u S.push(e.to) return visited

DFS Pseudocode

Page 39: Lecture 210 Ct 4

Graph Traversal (DFS)

13.11.2015 38

Data Fusion between a 2D Laser Profile Sensor and a Camera

DFS Undirected Graph

Output from our example graph is [A,B,S,C,D,E,H,G,F] when beginning with vertex A. What other order could it be?

Page 40: Lecture 210 Ct 4

Graph Traversal (DFS)

13.11.2015 39

Data Fusion between a 2D Laser Profile Sensor and a Camera

DFS Directed Graph

Page 41: Lecture 210 Ct 4

Graph Traversal (DFS)

13.11.2015 40

Data Fusion between a 2D Laser Profile Sensor and a Camera

• Output from the example graph is [1, 7, 5, 8, 6, 4, 3] when beginning with vertex 1. 2 is never visited because no edges go to it.

• Could have been different, but the edge ordering from each node is arbitrary.

• What other order could it be?

DFS Pseudocode

Page 42: Lecture 210 Ct 4

Graph Traversal (BFS)

Data Fusion between a 2D Laser Profile Sensor and a Camera

13.11.2015 41

Breadth-First Search (BFS)

• Traverses the graph by searching every edge from the current vertex.

• Sometimes called Breadth First Traversal (BFT). • Only moves to the next vertex in the graph when all edges have

been explored. • The algorithm is the same as DFS except that a Queue is used to

store the list of vertices to visit. • Will find the shortest path.

Page 44: Lecture 210 Ct 4

Graph Traversal (BFS)

Data Fusion between a 2D Laser Profile Sensor and a Camera

13.11.2015 43

BFS Psedocode

BREADTH-FIRST-SEARCH(G,v) Q←new Queue() visited←[] Q.enqueue(v) while Q is not empty u←q.dequeue() if u is not in visited visited.append(u) for all edges, e, from u Q.enqueue(e.to) return visited

Page 45: Lecture 210 Ct 4

Graph Traversal (BFS)

13.11.2015 44

Data Fusion between a 2D Laser Profile Sensor and a Camera

BFS Undirected Graph

Output from our example graph is [A,B,S,C,G,D,E,F,H] when beginning with vertex A. What other order could it be?

Page 46: Lecture 210 Ct 4

Dijkstra’s Algorithm

13.11.2015 45

Data Fusion between a 2D Laser Profile Sensor and a Camera

Dijkstra’s Algorithm

• Finds shortest path in a weighted graph. • We start by setting a starting value for each vertex, 0 for the current one and infinity for all others. • We also have a set for visited nodes and a set of tentative weights for unvisited nodes. • Then we scan the neighbours of the source node and update the tentative distance to each of its neighbour nodes so that it is equal to the weight of the edge from the source to the neighbour the unvisited node with the minimum tentative weight now has its shortest path calculated.

Page 47: Lecture 210 Ct 4

Dijkstra’s Algorithm

13.11.2015 46

Data Fusion between a 2D Laser Profile Sensor and a Camera

Dijkstra’s Algorithm

• Having found the shortest path to the node with minimum tentative weight, we then add it to the scanned set and then update the tentative weights of all the neighbours as follows: • v.tw is the tentative weight of vertex v • u[v] is the edge from u to v • x.w is the weight of edge x

Page 48: Lecture 210 Ct 4

Dijkstra’s Algorithm

13.11.2015 47

Data Fusion between a 2D Laser Profile Sensor and a Camera

Dijkstra’s Algorithm

• http://optlab-server.sce.carleton.ca/POAnimations2007/DijkstrasAlgo.html

• http://visualgo.net/sssp.html • https://www.cs.usfca.edu/~galles/visualization/Dijkstra.html • https://www.youtube.com/watch?v=UG7VmPWkJmA

Page 49: Lecture 210 Ct 4

13.11.2015 48

Data Fusion between a 2D Laser Profile Sensor and a Camera

Page 50: Lecture 210 Ct 4

13.11.2015 49

Data Fusion between a 2D Laser Profile Sensor and a Camera

Actual homework

• For the graph you have already implemented, implement either depth first or breadth first search. You may use the stack or queues from a library or your own implementation.

Page 51: Lecture 210 Ct 4

13.11.2015 50

Data Fusion between a 2D Laser Profile Sensor and a Camera

Alternative homework

• Adapt your graph to support weighted connections. • Implement Dijkstra’s algorithm.