Graph BichTra

You might also like

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 27

Graphs

1843 ORD
SFO

802
4 3
17

337
1233
LAX DFW

© 2013 Goodrich, Tamassia, Goldwasser 1


Graphs: Introduction
Graphs are non-linear data structures made up of two
major components:
Vertices – Vertices are entities in a graph. Every vertex has a
value associated with it.
 For example, if we represent a list of cities using a graph, the vertices
would represent the cities.
Edges – Edges represent the relationship between the vertices
in the graph. Edges may or may not have a value associated
with them.
 For example, if we represent a list of cities using a graph, the edges
would represent the path between the cities.

© 2013 Goodrich, Tamassia, Goldwasser 2


Graph

© 2013 Goodrich, Tamassia, Goldwasser 3


Graphs
 A graph is a pair (V, E), where
 V is a set of nodes, called vertices
 E is a collection of pairs of vertices, called edges
 Vertices and edges are positions and store elements
 Example:
 A vertex represents an airport and stores the three-letter airport code
 An edge represents a flight route between two airports and stores the
mileage of the route
849 PVD
1843 ORD 2
SFO 14

7 43 802 LGA
337

1 8 7 10
2555 13
HNL 1233 99
LAX DFW 1120
MIA
© 2013 Goodrich, Tamassia, Goldwasser 4
Applications
cslab1a cslab1b
 Electronic circuits math.brown.edu
 Printed circuit board
 Integrated circuit
cs.brown.edu
 Transportation networks
 Highway network brown.edu
 Flight network qwest.net
att.net
 Computer networks
 Local area network
 Internet cox.net
 Web John

 Databases Paul
David
 Entity-relationship diagram
© 2013 Goodrich, Tamassia, Goldwasser 5
Graph Types
 There are many types of graphs, based
on weights, direction, interconnectivity,
and special properties. Let’s look at the
most common types of graphs.
 Based on Direction
 Undirected Graphs
 Directed Graphs
 Based on Weights
 Weighted Graphs
 Unweighted Graphs
© 2013 Goodrich, Tamassia, Goldwasser 6
Undirected Graphs
 In an undirected graph, the edges have
no path or direction.
 If there is a path from vertex X to
vertex Y, then there is a path from
vertex Y to vertex X.
 Edge (X, Y) represents the edge
connecting vertex X to vertex Y.

© 2013 Goodrich, Tamassia, Goldwasser 7


Undirected Graphs
 Example

© 2013 Goodrich, Tamassia, Goldwasser 8


Directed Graphs
 In a directed graph or digraph, the
edges have an orientation.
 If there is a path from vertex X to
vertex Y, then there isn’t necessarily a
path from vertex Y to vertex X.

© 2013 Goodrich, Tamassia, Goldwasser 9


Directed Graphs

© 2013 Goodrich, Tamassia, Goldwasser 10


Weighted Graphs
 A weighted graph has a value
associated with every edge. 
 The value may represent quantities like
cost, distance, time, etc., depending on
the graph. An edge of a weighted graph
is represented as, (u, v, w).

© 2013 Goodrich, Tamassia, Goldwasser 11


Weighted Graphs
 Ex

© 2013 Goodrich, Tamassia, Goldwasser 12


Unweighted Graphs
 An unweighted graph does not have a
value associated with every edge. An
edge of an unweighted graph is
represented as, (u, v).

© 2013 Goodrich, Tamassia, Goldwasser 13


Unweighted Graphs

© 2013 Goodrich, Tamassia, Goldwasser 14


Representing Graphs
 There are multiple ways of using data
structures to represent a graph. The
three most common ways are:
 Adjacency Matrix
 Adjacency List
 Edge List

© 2013 Goodrich, Tamassia, Goldwasser 15


Adjacency Matrix
 In an unweighted graph, the element A[i]
[j] represents a Boolean value that
determines if a path exists from vertex i to
vertex j.
 If A[i][j] == 0, then no path from vertex i
to vertex j exists.
 If A[i][j] == 1, there is a path from vertex
i to vertex j.

© 2013 Goodrich, Tamassia, Goldwasser 16


Adjacency Matrix
 An Adjacency Matrix is a very simple way
to represent a graph.
 In a weighted graph, the element A[i][j]
represents the cost of moving from vertex
i to vertex j.

© 2013 Goodrich, Tamassia, Goldwasser 17


Example
 Example: The adjacency matrix of the
following graph is:

© 2013 Goodrich, Tamassia, Goldwasser 18


Example
 Example: The adjacency matrix of the
following graph is:

© 2013 Goodrich, Tamassia, Goldwasser 19


Exercise
 Create this graph using an adjacency
matrix and then show all the edges that
exist in the graph.

© 2013 Goodrich, Tamassia, Goldwasser 20


Adjacency List
 An adjacency list represents a graph as
a list that has vertex-edge mappings.
 Example, A → [(B, 4), (C, 1)]
represents an adjacency list where the
vertex A is connected to B (weight 4)
and C (weight 1).
 This works really well for sparse graphs.

© 2013 Goodrich, Tamassia, Goldwasser 21


Adjacency List
 Example

© 2013 Goodrich, Tamassia, Goldwasser 22


Adjacency List
 Example

© 2013 Goodrich, Tamassia, Goldwasser 23


Exercises

© 2013 Goodrich, Tamassia, Goldwasser 24


Graph Traversals
 Graph traversal means visiting every vertex
and edge exactly once in a well-defined
order.
 While using certain graph algorithms, you
must ensure that each vertex of the graph
is visited exactly once. The most common
way of tracking vertices is to mark them.
 There are many ways to traverse graphs
© 2013 Goodrich, Tamassia, Goldwasser 25
Breadth First Search (BFS)
 BFS is a traversing algorithm where you
should start traversing from a selected
node (source or starting node) and traverse
the graph layerwise thus exploring the
neighbour nodes (nodes which are directly
connected to source node).
 You must then move towards the next-level
neighbour nodes.

© 2013 Goodrich, Tamassia, Goldwasser 26


Breadth First Search (BFS)
1. First move horizontally and visit all the
nodes of the current layer
2. Move to the next layer

© 2013 Goodrich, Tamassia, Goldwasser 27

You might also like