It's all about relationships
You'll revisit these!
You'll revisit these!
Get ready for some MATH!
Graphs we've seen
data
data
data
data
undefined
Directed Acyclic Graph
Linear Data Structure
Graphs we've seen
Directed Acyclic Graph
Non-Linear Data Structure
a
c
b
d
g
f
h
e
i
j
Undirected, Cyclic Graph
This is a Node
Nodes represent entities in graphs
Node(s) || Vertex (Vertices)
Node |
---|
data |
edges |
A Node class should have:
This is an Edge
Edges represent relationships in graphs
Edge
Node |
---|
data |
edges |
Nodes keep track of Edges
Directed / Undirected Graphs
This is an Edge with no direction
This is an Edge with a direction
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
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
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
a
c
b
d
g
f
h
e
i
j
Friendship!
An edge here represents a mutual friendship, such as a Facebook friendship
var User = function(name) {
this.name = name;
this.friends = [];
}
User.prototype.addFriend(user) {
this.friends.push(user);
user.friends.push(this);
}
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
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
Acyclic
a
c
b
a
c
d
b
e
Connected
Disconnected
a
c
b
a
c
d
b
e
e
Complete
Incomplete
a
c
b
a
c
d
b
e
e
a
c
b
d
g
f
h
e
i
j
15
37
-12
5
3
23
1
-3
-15
-20
-44
15
15
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(
)
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
Draw an undirected graph
Everyone Draws: 2 mins
Draw one of the examples of undirected graphs from the earlier discussion
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
Draw a directed graph
Draw one of the real-world examples of directed graphs from the earlier discussion
Everyone Draws: 2 mins
What does it mean for a graph to be Weighted?
When might we use Weights on our edges?
Turn and Talk: 2 mins
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
Here's how to read a textbook
This is a graph in mathematical notation
Represents the whole graph
Refers to the vertices in the graph
Refers to the edges in the graph
Here's how to read a textbook
Is an ordered pair
Is an unordered set
Here's how to read a textbook
a
b
c
d
an undirected cyclic graph
Unordered pairs mean that the edge is not explicitly one-way
Here's how to read a textbook
a
b
c
d
a directed acyclic graph
{
}
Ordered pairs mean we are making explicit connections in one direction
Everybody Draws: 5 mins
Given:
Draw a graph such that:
Then use Graph Vocab to describe this graph
Everybody Draws: 5 mins
Given:
Draw a graph such that:
Then use Graph Vocab to describe this graph
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