james tam introduction to graphs in this section of notes you will learn about a new adt: graphs

32
James Tam Introduction To Graphs •In this section of notes you will learn about a new ADT: graphs.

Post on 19-Dec-2015

215 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: James Tam Introduction To Graphs In this section of notes you will learn about a new ADT: graphs

James Tam

Introduction To Graphs

•In this section of notes you will learn about a new ADT: graphs.

Page 2: James Tam Introduction To Graphs In this section of notes you will learn about a new ADT: graphs

James Tam

Graphs Are Related To Trees

•Like a tree a graph consists of nodes (vertex) and arcs (edges) that connect the nodes

•Unlike a tree there is no “up/down” direction (no parent-child relation), there is no root node

Calgary

Edmonton

Fort McMurray

Banff

Lethbridge

Start?

Start?

Start?Start?

Start?

Page 3: James Tam Introduction To Graphs In this section of notes you will learn about a new ADT: graphs

James Tam

Graph Terminology

•Adjacent nodes

•Cycle

•Acyclic graph

•Sub-graph

•Connected/disconnected graphs

•Complete graphs

•Directed/undirected graphs

•Weighted graphs

Page 4: James Tam Introduction To Graphs In this section of notes you will learn about a new ADT: graphs

James Tam

Adjacent Nodes

•Nodes are adjacent if they are connected by an edge

A

B

C

D

E

Adjacent pairs(a, b)

(a, d)

(b, c)

(c, d)

(d, e)

Page 5: James Tam Introduction To Graphs In this section of notes you will learn about a new ADT: graphs

James Tam

Cycle

•A path that begins and ends with the same node

A

B

C

D

E

Page 6: James Tam Introduction To Graphs In this section of notes you will learn about a new ADT: graphs

James Tam

Acyclic Graph

•Has no cycles

A

B

C

D

E

Page 7: James Tam Introduction To Graphs In this section of notes you will learn about a new ADT: graphs

James Tam

Sub-Graph

•A portion of a graph that is also a graph

A

D

E

A

B

C

D

E

Page 8: James Tam Introduction To Graphs In this section of notes you will learn about a new ADT: graphs

James Tam

Connected Graphs

•You can go from any node to any other node by following the edges

Note: It is not a requirement for connected graphs to have edges from every pair of nodes

Page 9: James Tam Introduction To Graphs In this section of notes you will learn about a new ADT: graphs

James Tam

Disconnected Graphs

•Some nodes are unreachable because there is no edge that connects them with the rest of the graph

Page 10: James Tam Introduction To Graphs In this section of notes you will learn about a new ADT: graphs

James Tam

Complete Graphs

•Every pair of nodes has an edge between them (every node is directly connected to every other node)

Page 11: James Tam Introduction To Graphs In this section of notes you will learn about a new ADT: graphs

James Tam

Directed Graphs

•Traversal between nodes is not guaranteed to be symmetric- E.g., map information that represents one way streets

Page 12: James Tam Introduction To Graphs In this section of notes you will learn about a new ADT: graphs

James Tam

Undirected Graphs

•Each connection is symmetric (a connection in one direction guarantees a connection in other direction)

Page 13: James Tam Introduction To Graphs In this section of notes you will learn about a new ADT: graphs

James Tam

Weighted Graph

•Shows the cost of traversing an edge

•Costs of traveling between cities- Distance in kilometers- Travel time in hours- Dollar cost of a taxi- Etc.

Calgary

Edmonton

Banff

Lethbridge

300

100

150

Page 14: James Tam Introduction To Graphs In this section of notes you will learn about a new ADT: graphs

James Tam

Comparing Trees And Graphs Again

• A Tree is A More Specific Form Of Graph

• A typical1 tree is a graph that has the following characteristics1. It is connected2. It has no cycles1

3. There is an up/down direction (there is a parent-child relation between nodes)

4. One node is treated as the top (the root node has no parent node)

Root

1 The type of tree that you were required to implement was somewhat rare

Page 15: James Tam Introduction To Graphs In this section of notes you will learn about a new ADT: graphs

James Tam

Graph Implementations

Graph

Adjacency matrix (Array)

Adjacency list (Linked list)

ADT (general concept)

Data structure (specific)

Page 16: James Tam Introduction To Graphs In this section of notes you will learn about a new ADT: graphs

James Tam

Adjacency Matrix: Array Implementation

A D G

B E H

C F I

ADT: Graph

Data structure: A 2D square array

•No rows = no columns = no. of nodes in the graph

A B C D E F G H I

A

B

C

D

E

F

G

H

I

Page 17: James Tam Introduction To Graphs In this section of notes you will learn about a new ADT: graphs

James Tam

Possible Array Implementations

T T T

T

T

T

T T

T T

T

T

T

A 2D array of boolean values

A B C D E F G H I

A

B

C

D

E

F

G

H

I

1 1 1

1

1

1

1 1

1 1

1

1

1

A B C D E F G H I

A

B

C

D

E

F

G

H

I

A 2D array of integer values

Page 18: James Tam Introduction To Graphs In this section of notes you will learn about a new ADT: graphs

James Tam

A Linked List Implementation Of A Graph

GDA

HEB

IFC

Page 19: James Tam Introduction To Graphs In this section of notes you will learn about a new ADT: graphs

James Tam

The List Of Edges Must Be Dynamic1

A

D

G

B

E

H

C

F

I

BDE

E

B

G

FH

CH

H

I

F

1 Some sort of resizable list is needed e.g., a linked list or an array that can change in size

Page 20: James Tam Introduction To Graphs In this section of notes you will learn about a new ADT: graphs

James Tam

An Outline For A Node

class Node

{

private dataType data;

private boolean visited;

Dynamic list of connections;

: : :

}

Page 21: James Tam Introduction To Graphs In this section of notes you will learn about a new ADT: graphs

James Tam

Graph Traversals

•Breadth first

•Depth first

Page 22: James Tam Introduction To Graphs In this section of notes you will learn about a new ADT: graphs

James Tam

Breadth-First Traversals

•Visit a node (N)

•Visit all of the nodes that node N refers to before following the second level of references

N1st

:

L1(a) L1 (b)2nd 3rdFirst level of references

L2 (a)4thSecond level of references

Page 23: James Tam Introduction To Graphs In this section of notes you will learn about a new ADT: graphs

James Tam

Algorithm For Breadth-First Traversals

• In a fashion that is similar to breadth first traversals for trees, a queue is employed to store all the nodes that are adjacent to the node that is currently being visited.

breadthFirst (node)

{

Queue nodeList = new Queue ()

Node temp

Mark node as visited and display node

nodeList.enqueue(node)

Page 24: James Tam Introduction To Graphs In this section of notes you will learn about a new ADT: graphs

James Tam

Algorithm For Breadth-First Traversals (2)

while (queue.isEmpty() == false)

{

temp = nodeList.dequeue ()

for (each unvisisted node uNode that is adjacent to temp)

{

Mark uNode as visited

display uNode

nodeList.enqueue(uNode)

}

}

}

Page 25: James Tam Introduction To Graphs In this section of notes you will learn about a new ADT: graphs

James Tam

First Example Of A Breadth First Traversal

v

u

q

r s

t

w x

Starting pointFirst level

Second level

Third level

Fourth level

Page 26: James Tam Introduction To Graphs In this section of notes you will learn about a new ADT: graphs

James Tam

Second Example Of A Breadth-First Traversal

A

B

C

D

E

F

G

H

I

Q: What order do you get for a breadth-first traversal if the starting point is node E?

Starting point

Page 27: James Tam Introduction To Graphs In this section of notes you will learn about a new ADT: graphs

James Tam

Depth-First Traversals

•Visit a node

•Completely follow the series of references for a chain of nodes before visiting the second reference for that node

L1 (b)

:

N1st

L1(a)2nd

L2 (a)3rd

Page 28: James Tam Introduction To Graphs In this section of notes you will learn about a new ADT: graphs

James Tam

Algorithm For Depth-First Traversals

• Typically recursion is used (requires backtracking and the use of the system stack).

• If a loop is used then the programmer must create and manage his or her own stack.

depthFirst (node)

{

Display node

Mark node as visited

for (each unvisited node (uNode) that is adjacent to node)

depthFirst (node)

}

Page 29: James Tam Introduction To Graphs In this section of notes you will learn about a new ADT: graphs

James Tam

First Example Of A Depth First Traversal

v

u

q

r s

t

w x

Starting point

Page 30: James Tam Introduction To Graphs In this section of notes you will learn about a new ADT: graphs

James Tam

Second Example Of A Depth-First Traversal

A

B

C

D

E

F

G

H

I

Q: What order do you get for a depth-first traversal if the starting point is node E?

Starting point

Page 31: James Tam Introduction To Graphs In this section of notes you will learn about a new ADT: graphs

James Tam

You Should Now Know

•What is a graph

•Common graph definitions

•What are the different ways in which graphs can be implemented

•How do breadth-first and depth-first traversals work

Page 32: James Tam Introduction To Graphs In this section of notes you will learn about a new ADT: graphs

James Tam

Sources Of Lecture Material

•“Data Structures and Abstractions with Java” by Frank M. Carrano and Walter Savitch

•“Data Abstraction and Problem Solving with Java: Walls and Mirrors” by Frank M. Carrano and Janet J. Prichard

•CPSC 331 course notes by Marina L. Gavrilova http://pages.cpsc.ucalgary.ca/~marina/331/