Graphs

It's all about relationships

Objectives

  1. Use Graph Theory vocabulary
  2. Use Graph Theory Notation
  3. Model Real World Relationships with Graphs

You'll revisit these!

You need:

  • Whiteboards
  • Whiteboard Markers
  • Paper to take notes on Vocab Words, and Notation

You'll revisit these!

Get ready for some MATH!

Graph Theory

Graphs we've seen

data

data

data

data

undefined

Directed Acyclic Graph

Linear Data Structure

Graph Theory

Graphs we've seen

Directed Acyclic Graph

Non-Linear Data Structure

Graphs

a

c

b

d

g

f

h

e

i

j

Undirected, Cyclic Graph

Graph Vocab

This is a Node

Nodes represent entities in graphs

Node(s) || Vertex (Vertices)

Node
data
edges

A Node class should have:

  • content
  • relationships

Graph Vocab

This is an Edge

Edges represent relationships in graphs

Edge

Node
data
edges

Nodes keep track of Edges

Graph Vocab

Directed / Undirected Graphs

This is an Edge with no direction

This is an Edge with a direction

Graph Vocab

a

c

b

d

g

f

h

e

i

j

This is an Undirected graph

It's edges have no direction

A relationship is two-way by default

Graph Vocab

a

c

b

d

g

f

h

e

i

j

This is a Directed Graph

A relationship is explicitly one way

To have a two way relationship, a new relationship must be formed

Edges

How to represent

var Node = function (data) {
    this.data = data;
    this.edges = [];
}

Node.prototype.addEdge = function(node) {
    this.edges.push(node);
    node.edges.push(this);
}

with Pointers

Example: Undirected Graph

a

c

b

d

g

f

h

e

i

j

Friendship!

An edge here represents a mutual friendship, such as a Facebook friendship

Example: Undirected Graph

var User = function(name) {
    this.name = name;
    this.friends = [];
}

User.prototype.addFriend(user) {
    this.friends.push(user);
    user.friends.push(this);
}

Example: Directed Graph

a

c

b

d

g

f

h

e

Followers on Twitter

q

x

z

b

a is pretty popular, as is e

f, c, and d follow a and e, but are not followed back

b does not follow anyone, but is followed by g, who also follows e

Example: Directed Graph

Followers on Twitter

var User = function(name) {
    this.name = name;
    this.followers = [];
    this.following = [];
}

User.prototype.follow = function (user) {
    this.following.push(user);
    user.followers.push(this);
}

Cyclic vs Acyclic

Cyclic

Acyclic

a

c

b

a

c

d

b

e

Connected vs Disconnected

Connected

Disconnected

a

c

b

a

c

d

b

e

e

Complete vs Incomplete

Complete

Incomplete

a

c

b

a

c

d

b

e

e

Weights

a

c

b

d

g

f

h

e

i

j

15

37

-12

5

3

23

1

-3

-15

-20

-44

15

15

Weights

a

b

15

3

c

d

2

10

2 possible paths from a to d

a

b

d

a

c

d

Total Weight: 5

Total Weight: 25

Shortest Path:

a

b

d

sum(

)

sum(

)

Objectives

  1. Use Graph Theory vocabulary
  2. Use Graph Theory Notation
  3. Model Real World Relationships with Graphs

Discuss:

What does it mean for a Graph to be Undirected?

What is a real-world example of an Undirected Graph?

Turn and Talk: 2 mins

Whiteboards

Draw an undirected graph

Everyone Draws: 2 mins

Draw one of the examples of undirected graphs from the earlier discussion

Discuss:

What does it mean for a Graph to be Directed?

What is a real-world example of a Directed Graph?

Turn and Talk: 2 mins

Whiteboards

Draw a directed graph

Draw one of the real-world examples of directed graphs from the earlier discussion

Everyone Draws: 2 mins

Discuss:

What does it mean for a graph to be Weighted?

When might we use Weights on our edges?

Turn and Talk: 2 mins

Whiteboards

Draw an undirected, weighted graph

Use an undirected weighted graph to represent major cities in the US, then use their distance from each other as weights

Everyone Draws: 4 mins

Graph Notation

Here's how to read a textbook

G = (V, E)
G=(V,E)G = (V, E)

This is a graph in mathematical notation

Represents the whole graph

G
GG
V
VV

Refers to the vertices in the graph

E
EE

Refers to the edges in the graph

Graph Notation

Here's how to read a textbook

(V, E)
(V,E)(V, E)

Is an ordered pair

\{a, b, c\}
{a,b,c}\{a, b, c\}

Is an unordered set

Graph Notation

Here's how to read a textbook

a

b

c

d

an undirected cyclic graph

G = (V, E)
G=(V,E)G = (V, E)
V =
V=V =
\{a, b, c, d\}
{a,b,c,d}\{a, b, c, d\}
E =
E=E =
\{\{a,b\}, \{b,d\}, \{d,c\}, \{a,c\}\}
{{a,b},{b,d},{d,c},{a,c}}\{\{a,b\}, \{b,d\}, \{d,c\}, \{a,c\}\}

Unordered pairs mean that the edge is not explicitly one-way

Graph Notation

Here's how to read a textbook

a

b

c

d

a directed acyclic graph

G = (V, E)
G=(V,E)G = (V, E)
V =
V=V =
a, b, c, d
a,b,c,da, b, c, d

{

}

E =
E=E =
\{(b,a), (d,b), (d,c), (c,a)\}
{(b,a),(d,b),(d,c),(c,a)}\{(b,a), (d,b), (d,c), (c,a)\}

Ordered pairs mean we are making explicit connections in one direction

Objectives

  1. Use Graph Theory vocabulary
  2. Use Graph Theory Notation
  3. Define and describe the relationships depicted in a Graph

Whiteboards

Everybody Draws: 5 mins

G = (V, E)
G=(V,E)G = (V, E)

Given:

Draw a graph such that:

V = \{x, z, a, b, c\}
V={x,z,a,b,c}V = \{x, z, a, b, c\}
E = \{\{a,b\}, \{b,z\}, \{x,c\}, \{a,c\}\}
E={{a,b},{b,z},{x,c},{a,c}}E = \{\{a,b\}, \{b,z\}, \{x,c\}, \{a,c\}\}

Then use Graph Vocab to describe this graph

Whiteboards

Everybody Draws: 5 mins

G = (V, E)
G=(V,E)G = (V, E)

Given:

Draw a graph such that:

V = \{x, z, a, b, c\}
V={x,z,a,b,c}V = \{x, z, a, b, c\}
E = \{(b,a), (x,b), (x,c), (z,a)\}
E={(b,a),(x,b),(x,c),(z,a)}E = \{(b,a), (x,b), (x,c), (z,a)\}

Then use Graph Vocab to describe this graph

Objectives

  1. Use Graph Theory vocabulary
  2. Use Graph Theory Notation
  3. Model Real World Relationships with Graphs

Exercise:

10 Minutes (in pairs): Draw a graph that represents all of the pairs that you or your partner have been a part of (in terms of pair programming)

2 Minutes: Describe the graph with Graph Vocabulary

5 Minutes: Use Graph Notation to describe the graph

Graph Theory

By Hamid Aghdaee

Graph Theory

  • 729