4. ICSE: Introduction to Computer Sciences for Engineers – Lecture 09
ICSE
4
Graphs: Types and Properties
What is a Graph?
• a graph consists of:
• a set of vertices (also known as nodes)
• a set of edges (also known as arcs), each of which connects a pair
of vertices
d
e
f
g
c i
a
b h j
5. ICSE: Introduction to Computer Sciences for Engineers – Lecture 09
ICSE
5
Graphs: Types and Properties
Example: A Highway Graph
• vertices represent cities.
• edges represent highways.
• this is a weighted graph, because it has a cost associated with each edge.
• for this example, the costs denote distance (in kilometres)
• we’ll use graph algorithms to answer questions like
• “What is the shortest route from Berlin to Hannover?”
Berlin
Magdeburg
Hamburg
Potsdam
Dresden
Hannover
203
3
5
156
112
149
127
151
Bremen
Leipzig
125
127
125 291
6. ICSE: Introduction to Computer Sciences for Engineers – Lecture 09
ICSE
6
Graphs: Types and Properties
Relationship among Vertices
• two vertices are adjacent if they are connected by a single edge.
• the collection of vertices that are adjacent to a vertex v are
referred to as v’s neighbors
• c’s neighbors are a, b, d, f, and g
d
e
f
g
c i
a
b h j
not adjacent
adjacent
7. ICSE: Introduction to Computer Sciences for Engineers – Lecture 09
ICSE
7
Graphs: Types and Properties
Paths in a Graph
• a path is a sequence of edges that connects two vertices.
• the path highlighted below connects c and e,
d
e
f
g
c i
a
b h j
8. ICSE: Introduction to Computer Sciences for Engineers – Lecture 09
ICSE
8
Graphs: Types and Properties
Paths in a Graph
• a path is a sequence of edges that connects two vertices.
• a graph is connected if there is a path between any two vertices
• a graph is complete if there is an edge between every pair of
vertices.
b
e
d
c
a
a
b
c
d
e
the graph is
• connected
• complete
the graph is
• not connected
• not complete
9. ICSE: Introduction to Computer Sciences for Engineers – Lecture 09
ICSE
9
Graphs: Types and Properties
Directed Graphs
• a directed path has a direction associated with each edge, which is depicted using an
arrow
• edges in a directed graph are often represented as ordered pairs of the form (start
vertex, end vertex)
• (a,b) is an edge in the graph above, but (b,a) is not
b
e
d
c
a f
10. ICSE: Introduction to Computer Sciences for Engineers – Lecture 09
ICSE
10
Graphs: Types and Properties
Directed Graphs
• a directed path has a direction associated with each edge, which is depicted using
an arrow
• a path in a directed graph is a sequence of edges in which the end vertex of edge i
must be the same as the start vertex of edge i+1.
b
e
d
c
a f
11. ICSE: Introduction to Computer Sciences for Engineers – Lecture 09
ICSE
11
Graphs: Types and Properties
Directed Graphs
• a directed path has a direction associated with each edge, which is depicted using an
arrow
• a path in a directed graph is a sequence of edges in which the end vertex of edge i must
be the same as the start vertex of edge i+1.
• {(a,b),(b,e),(e,f)} is a valid path
e
d
c f
b
a
12. ICSE: Introduction to Computer Sciences for Engineers – Lecture 09
ICSE
12
Graphs: Types and Properties
Directed Graphs
• a directed path has a direction associated with each edge, which is depicted using an arrow
• a path in a directed graph is a sequence of edges in which the end vertex of edge i must be
the same as the start vertex of edge i+1.
• {(a,b),(b,e),(e,f)} is a valid path
• {(a,b),(c,b),(c,a)} is not a valid path
b
e
d
f
a c
13. ICSE: Introduction to Computer Sciences for Engineers – Lecture 09
ICSE
13
Graphs: Types and Properties
Trees vs. Graphs
• a tree is a special type of graph.
• it is connected and undirected
• it is acyclic: there is no path containing distinct edges that starts
and ends at the same vertex
• we usually single out one of the vertices to be the root of the tree,
although graph theory does not require this
d
e
f
i
b h
c
a
d
e
f
c i
a
b h
a graph that is not a tree, with one cycle
highlighted
a tree using the same nodes
14. ICSE: Introduction to Computer Sciences for Engineers – Lecture 09
ICSE
14
d
e
f
c i
a
b h
Graphs: Types and Properties
Trees vs. Graphs
• a tree is a special type of graph.
• it is connected and undirected
• it is acyclic: there is no path containing distinct edges that starts
and ends at the same vertex
• we usually single out one of the vertices to be the root of the tree,
although graph theory does not require this
d
e
f
i
h
c
b
a
a graph that is not a tree, with one cycle
highlighted
another tree using the same nodes
15. ICSE: Introduction to Computer Sciences for Engineers – Lecture 09
ICSE
15
Spanning Trees
• a spanning tree is a subset of a connected graph that contains:
• all of the vertices
• a subset of the edges that form a tree
• the trees on the previous page were examples of spanning trees for the
graph on that page. Here are two others:
Graphs: Types and Properties
d
e
f
c i
a
b h
the original graph another spanning tree for this graph
d
e
f
c i
a
b h
16. ICSE: Introduction to Computer Sciences for Engineers – Lecture 09
ICSE
16
Spanning Trees
• a spanning tree is a subset of a connected graph that contains:
• all of the vertices
• a subset of the edges that form a tree
• the trees on the previous page were examples of spanning trees for the
graph on that page. Here are two others:
Graphs: Types and Properties
d
e
f
c i
a
b h
the original graph another spanning tree for this graph
d
e
f
c i
a
b h
18. ICSE: Introduction to Computer Sciences for Engineers – Lecture 09
ICSE
18
Graphs: Representation
Edge List
• a list or array is used to represent the edges
• Example: Directed Graph
4
5
2
3
6
1
int [] edgelist = {6,11, 1,2,1,3,3,1,3,4,3,6,4,1,5,3,5,5,6,2,6,4,6,5}
number of vertices number of edges
19. ICSE: Introduction to Computer Sciences for Engineers – Lecture 09
ICSE
19
Graphs: Representation
Node List
• a list or array is used to represent the neighborhoods of an vertex
• Example: Directed Graph
1
4
6
5
2
3
int [] nodelist = {6,11, 2,2,3, 0, 3,1,4,6, 1,1, 2,3,5, 3,2,4,5}
number of vertices number of edges
number of edges of the vertex 6
20. ICSE: Introduction to Computer Sciences for Engineers – Lecture 09
ICSE
20
Graphs: Representation
Node List
• a list or array is used to represent the neighborhoods of an vertex
• Example: Directed Graph
• neighborhood of vertex i: nodeNhd[i-1], … , nodeNhd[i]-1
• number of vertices: nodeNhd.length-1
• number of edges: nodeNhd[nodeNhd.length-1]
int [] nodelist = {6,11, 2,2,3, 0, 3,1,4,6, 1,1, 2,3,5, 3,2,4,5};
int [] neighborhoods = {6,11, 2,2,3, 0, 3,1,4,6, 1,1, 2,3,5, 3,2,4,5};
int [] nodeNhd = { 0, 2, 2, 5, 6, 8, 11};
21. ICSE: Introduction to Computer Sciences for Engineers – Lecture 09
ICSE
21
Graphs: Representation
Adjacency Matrix
• a two-dimensional array that is used to represent the edges
• edge[r][c] = the edge between the vertices r and c
• Example: Undirected Graph
• use a special value 0 to indicate that you can’t go from r to c.
1 2 3 4 5 6
1 0 1 1 1 0 0
2 1 0 0 0 0 1
3 1 0 0 1 1 1
4 1 0 1 0 0 1
5 0 0 1 0 0 1
6 0 1 1 1 1 0
1
5
2
3 4
6
22. ICSE: Introduction to Computer Sciences for Engineers – Lecture 09
ICSE
22
Graphs: Representation
Adjacency Matrix
• a two-dimensional array that is used to represent the edges
• edge[r][c] = the edge going from vertex r to vertex c
• Example: Directed Graph
• use a special value 0 to indicate that you can’t go from r to c.
1 2 3 4 5 6
1 0 1 1 0 0 0
2 0 0 0 0 0 0
3 1 0 0 1 0 1
4 1 0 0 0 0 0
5 0 0 1 0 1 0
6 0 1 0 1 1 0
1
4
6
5
2
3
23. ICSE: Introduction to Computer Sciences for Engineers – Lecture 09
ICSE
23
Graphs: Representation
Adjacency Matrix
• a two-dimensional array that is used to represent the edges and any associated costs
• edge[r][c] = the cost of going from vertex r to vertex c
• Example: Weighted Graph
• use a special value to indicate that you can’t go from r to c.
• either there’s no edge between r and c, or it’s a directed edge that goes from c to r
• this value is shown as a shaded cell in the matrix above
• we can’t use 0, because we may have actual costs of 0
0 1 2 3
0 156 35
1 156 125
2 35 125 203
3 203
0. Berlin
1. Magdeburg 2. Potsdam
3. Dresden
2
0
3
3
5
156
125
24. ICSE: Introduction to Computer Sciences for Engineers – Lecture 09
ICSE
24
Graphs: Representation
Adjacency Matrix
• a two-dimensional array that is used to represent the edges and any associated costs
• edge[r][c] = the cost of going from vertex r to vertex c
• Example: Weighted Graph
• this representation
• is good if a graph is dense – if it has many edges per vertex
• wastes memory if the graph is sparse – if it has few edges per vertex
0 1 2 3
0 156 35
1 156 125
2 35 125 203
3 203
0. Berlin
1. Magdeburg 2. Potsdam
3. Dresden
2
0
3
3
5
156
125
25. ICSE: Introduction to Computer Sciences for Engineers – Lecture 09
ICSE
25
Graphs: Representation
Adjacency List
• a list (either an array or linked list) of linked lists that is used to represent the edges
• Example: Directed Graph
1
4
6
5
2
3
2
3
4
5
6
1 2
1
3
4 6
1
3 5
2 4 5
26. ICSE: Introduction to Computer Sciences for Engineers – Lecture 09
ICSE
26
Graphs: Representation
Adjacency List
• a list (either an array or linked list) of linked lists that is used to represent the edges and any associated
costs
• no memory is allocated for non-existent edges, but the references in the linked lists use extra memory.
• this representation is good if a graph is sparse, but wastes memory if the graph is dense.
35
2
0
1
2
3
null
156
1
125
2
35
0
125
1
null
203
3
null
203
2
null
156
0
0. Berlin
1. Magdeburg 2. Potsdam
3. Dresden
2
0
3
3
5
156
125
27. ICSE: Introduction to Computer Sciences for Engineers – Lecture 09
ICSE
27
Graphs: Representation
Adjacency List
• use a linked list of linked lists for the adjacency list.
0. Berlin
1. Magdeburg 2. Potsdam
3. Dresden
2
0
3
3
5
156
125
• vertices is a reference to a
linked list of vertex objects
• each vertex holds a reference
to a linked list of edge objects
• each edge holds a reference to the
vertex that is the end vertex.
35
null
156
null
Magdeburg
Potsdam
Dresden
Berlin
35 125
null
203
125
null
156
null
203
vertices
28. ICSE: Introduction to Computer Sciences for Engineers – Lecture 09
ICSE
28
Graphs: Representation
Adjacency List
• Class: private class Graph()
public class Graph {
private class Vertex {
private String id;
private Edge edges;
private Vertex next;
private boolean encountered;
private boolean done;
private Vertex parent;
private double cost;
…
}
private class Edge {
private Vertex start;
private Vertex end;
private double cost;
private Edge next;
…
}
private Vertex vertices;
…
}
next
cost
end
null
156
null
Dresden
next
edges
ID
null
203
vertices
30. ICSE: Introduction to Computer Sciences for Engineers – Lecture 09
ICSE
30
Traversing a Graph
• involves starting at some vertex and visiting all of the vertices that can be reached from that
vertex
• visiting a vertex = processing its data in some way
• example: print the data
• Depth-First Traversal / Search:
• proceed as far as possible along a given path before backing up
• Breadth-First Traversal / Search:
• visit a vertex
• visit all of its neighbors
• visit all unvisited vertices 2 edges away
• visit all unvisited vertices 3 edges away, etc.
• applications:
• determining the vertices that can be reached from some vertex
• state-space search
• web crawler (vertices = pages, edges = links)
Graphs: Traversal and Search
31. ICSE: Introduction to Computer Sciences for Engineers – Lecture 09
ICSE
31
Depth-First Traversal / Search
• visit a start vertex v, then make recursive calls on all of its yet-to-be-
visited neighbors:
• depthFirstTrav(v)
• visit vertex v and mark it as visited
• for each vertex w in v’s neighbors do
• if ( w has not been visited )
• depthFirstTrav(w)
• Depth-First Traversal / Search
• depthFirstTrav(1):
Graphs: Traversal and Search
1
6
7
2 8
5
3
4
32. ICSE: Introduction to Computer Sciences for Engineers – Lecture 09
ICSE
32
Depth-First Traversal / Search
• visit a start vertex v, then make recursive calls on all of its yet-to-be-visited
neighbors:
• depthFirstTrav(v)
• visit vertex v and mark it as visited
• for each vertex w in v’s neighbors do
• if ( w has not been visited )
• depthFirstTrav(w)
• Depth-First Traversal / Search
• depthFirstTrav(1):
1
Graphs: Traversal and Search
1
6
7
2 8
5
3
4
33. ICSE: Introduction to Computer Sciences for Engineers – Lecture 09
ICSE
33
Depth-First Traversal / Search
• visit a start vertex v, then make recursive calls on all of its yet-to-be-visited
neighbors:
• depthFirstTrav(v)
• visit vertex v and mark it as visited
• for each vertex w in v’s neighbors do
• if ( w has not been visited )
• depthFirstTrav(w)
• Depth-First Traversal / Search
• depthFirstTrav(1):
1
Graphs: Traversal and Search
1
6
7
2 8
5
3
4
34. ICSE: Introduction to Computer Sciences for Engineers – Lecture 09
ICSE
34
Depth-First Traversal / Search
• visit a start vertex v, then make recursive calls on all of its yet-to-be-visited
neighbors:
• depthFirstTrav(v)
• visit vertex v and mark it as visited
• for each vertex w in v’s neighbors do
• if ( w has not been visited )
• depthFirstTrav(w)
• Depth-First Traversal / Search
• depthFirstTrav(1):
1 3
Graphs: Traversal and Search
1
6
7
2 8
5
3
4
35. ICSE: Introduction to Computer Sciences for Engineers – Lecture 09
ICSE
35
Depth-First Traversal / Search
• visit a start vertex v, then make recursive calls on all of its yet-to-be-visited
neighbors:
• depthFirstTrav(v)
• visit vertex v and mark it as visited
• for each vertex w in v’s neighbors do
• if ( w has not been visited )
• depthFirstTrav(w)
• Depth-First Traversal / Search
• depthFirstTrav(1):
1 3
Graphs: Traversal and Search
1
6
2 8
5
3
4
7
36. ICSE: Introduction to Computer Sciences for Engineers – Lecture 09
ICSE
36
Depth-First Traversal / Search
• visit a start vertex v, then make recursive calls on all of its yet-to-be-visited
neighbors:
• depthFirstTrav(v)
• visit vertex v and mark it as visited
• for each vertex w in v’s neighbors do
• if ( w has not been visited )
• depthFirstTrav(w)
• Depth-First Traversal / Search
• depthFirstTrav(1):
1 3 7
Graphs: Traversal and Search
1
6
2 8
5
3
4
7
37. ICSE: Introduction to Computer Sciences for Engineers – Lecture 09
ICSE
37
Depth-First Traversal / Search
• visit a start vertex v, then make recursive calls on all of its yet-to-be-visited
neighbors:
• depthFirstTrav(v)
• visit vertex v and mark it as visited
• for each vertex w in v’s neighbors do
• if ( w has not been visited )
• depthFirstTrav(w)
• Depth-First Traversal / Search
• depthFirstTrav(1):
1 3 7
Graphs: Traversal and Search
1
6
2 8
5
3
4
7
38. ICSE: Introduction to Computer Sciences for Engineers – Lecture 09
ICSE
38
Depth-First Traversal / Search
• visit a start vertex v, then make recursive calls on all of its yet-to-be-visited
neighbors:
• depthFirstTrav(v)
• visit vertex v and mark it as visited
• for each vertex w in v’s neighbors do
• if ( w has not been visited )
• depthFirstTrav(w)
• Depth-First Traversal / Search
• depthFirstTrav(1):
1 3 7 5
Graphs: Traversal and Search
1
6
2 8
5
3
4
7
39. ICSE: Introduction to Computer Sciences for Engineers – Lecture 09
ICSE
39
Depth-First Traversal / Search
• visit a start vertex v, then make recursive calls on all of its yet-to-be-visited
neighbors:
• depthFirstTrav(v)
• visit vertex v and mark it as visited
• for each vertex w in v’s neighbors do
• if ( w has not been visited )
• depthFirstTrav(w)
• Depth-First Traversal / Search
• depthFirstTrav(1):
1 3 7 5
Graphs: Traversal and Search
1
6
2 8
5
3
4
7
40. ICSE: Introduction to Computer Sciences for Engineers – Lecture 09
ICSE
40
Depth-First Traversal / Search
• visit a start vertex v, then make recursive calls on all of its yet-to-be-visited
neighbors:
• depthFirstTrav(v)
• visit vertex v and mark it as visited
• for each vertex w in v’s neighbors do
• if ( w has not been visited )
• depthFirstTrav(w)
• Depth-First Traversal / Search
• depthFirstTrav(1):
1 3 7 5 4
Graphs: Traversal and Search
1
6
2 8
5
3
4
7
41. ICSE: Introduction to Computer Sciences for Engineers – Lecture 09
ICSE
41
Depth-First Traversal / Search
• visit a start vertex v, then make recursive calls on all of its yet-to-be-visited
neighbors:
• depthFirstTrav(v)
• visit vertex v and mark it as visited
• for each vertex w in v’s neighbors do
• if ( w has not been visited )
• depthFirstTrav(w)
• Depth-First Traversal / Search
• depthFirstTrav(1):
1 3 7 5 4
Graphs: Traversal and Search
1
6
2 8
5
3
4
7
42. ICSE: Introduction to Computer Sciences for Engineers – Lecture 09
ICSE
42
Depth-First Traversal / Search
• visit a start vertex v, then make recursive calls on all of its yet-to-be-visited
neighbors:
• depthFirstTrav(v)
• visit vertex v and mark it as visited
• for each vertex w in v’s neighbors do
• if ( w has not been visited )
• depthFirstTrav(w)
• Depth-First Traversal / Search
• depthFirstTrav(1):
1 3 7 5 4 6
Graphs: Traversal and Search
1
6
2 8
5
3
4
7
43. ICSE: Introduction to Computer Sciences for Engineers – Lecture 09
ICSE
43
Depth-First Traversal / Search
• visit a start vertex v, then make recursive calls on all of its yet-to-be-visited
neighbors:
• depthFirstTrav(v)
• visit vertex v and mark it as visited
• for each vertex w in v’s neighbors do
• if ( w has not been visited )
• depthFirstTrav(w)
• Depth-First Traversal / Search
• depthFirstTrav(1):
1 3 7 5 4 6
Graphs: Traversal and Search
1
6
2 8
5
3
4
7
44. ICSE: Introduction to Computer Sciences for Engineers – Lecture 09
ICSE
44
Depth-First Traversal / Search
• visit a start vertex v, then make recursive calls on all of its yet-to-be-visited
neighbors:
• depthFirstTrav(v)
• visit vertex v and mark it as visited
• for each vertex w in v’s neighbors do
• if ( w has not been visited )
• depthFirstTrav(w)
• Depth-First Traversal / Search
• depthFirstTrav(1):
1 3 7 5 4 6
Graphs: Traversal and Search
1
6
2 8
5
3
4
7
45. ICSE: Introduction to Computer Sciences for Engineers – Lecture 09
ICSE
45
Depth-First Traversal / Search
• visit a start vertex v, then make recursive calls on all of its yet-to-be-visited
neighbors:
• depthFirstTrav(v)
• visit vertex v and mark it as visited
• for each vertex w in v’s neighbors do
• if ( w has not been visited )
• depthFirstTrav(w)
• Depth-First Traversal / Search
• depthFirstTrav(1):
1 3 7 5 4 6 8
Graphs: Traversal and Search
1
6
2 8
5
3
4
7
46. ICSE: Introduction to Computer Sciences for Engineers – Lecture 09
ICSE
46
Depth-First Traversal / Search
• visit a start vertex v, then make recursive calls on all of its yet-to-be-visited
neighbors:
• depthFirstTrav(v)
• visit vertex v and mark it as visited
• for each vertex w in v’s neighbors do
• if ( w has not been visited )
• depthFirstTrav(w)
• Depth-First Traversal / Search
• depthFirstTrav(1):
1 3 7 5 4 6 8
Graphs: Traversal and Search
1
6
2 8
5
3
4
7
47. ICSE: Introduction to Computer Sciences for Engineers – Lecture 09
ICSE
47
Depth-First Traversal / Search
• visit a start vertex v, then make recursive calls on all of its yet-to-be-visited
neighbors:
• depthFirstTrav(v)
• visit vertex v and mark it as visited
• for each vertex w in v’s neighbors do
• if ( w has not been visited )
• depthFirstTrav(w)
• Depth-First Traversal / Search
• depthFirstTrav(1):
1 3 7 5 4 6 8
Graphs: Traversal and Search
1
6
2 8
5
3
4
7
48. ICSE: Introduction to Computer Sciences for Engineers – Lecture 09
ICSE
48
Depth-First Traversal / Search
• visit a start vertex v, then make recursive calls on all of its yet-to-be-visited
neighbors:
• depthFirstTrav(v)
• visit vertex v and mark it as visited
• for each vertex w in v’s neighbors do
• if ( w has not been visited )
• depthFirstTrav(w)
• Depth-First Traversal / Search
• depthFirstTrav(1):
1 3 7 5 4 6 8 2
Graphs: Traversal and Search
1
6
2 8
5
3
4
7
49. ICSE: Introduction to Computer Sciences for Engineers – Lecture 09
ICSE
49
Graphs: Traversal and Search
Depth-First Traversal / Search from Berlin
149 125
Berlin
Magdeburg
Hamburg
Potsdam
Dresden
Hannover
203
3
5
156
112
127
151
Bremen
Leipzig
127
125 291
For the examples, we’ll assume
that the edges in each vertex’s
adjacency list are sorted by
increasing edge cost.
50. ICSE: Introduction to Computer Sciences for Engineers – Lecture 09
ICSE
50
Graphs: Traversal and Search
Depth-First Traversal / Search from Berlin
149 125
Berlin
Magdeburg
Hamburg
Potsdam
Dresden
Hannover
203
3
5
156
112
127
151
Bremen
Leipzig
127
125 291
For the examples, we’ll assume
that the edges in each vertex’s
adjacency list are sorted by
increasing edge cost.
Berlin
51. ICSE: Introduction to Computer Sciences for Engineers – Lecture 09
ICSE
51
Graphs: Traversal and Search
Depth-First Traversal / Search from Berlin
149 125
Berlin
Magdeburg
Hamburg
Potsdam
Dresden
Hannover
203
3
5
156
112
127
151
Bremen
Leipzig
127
125 291
For the examples, we’ll assume
that the edges in each vertex’s
adjacency list are sorted by
increasing edge cost.
Berlin
Potsdam
52. ICSE: Introduction to Computer Sciences for Engineers – Lecture 09
ICSE
52
Graphs: Traversal and Search
Depth-First Traversal / Search from Berlin
149 125
Berlin
Magdeburg
Hamburg
Potsdam
Dresden
Hannover
203
3
5
156
112
127
151
Bremen
Leipzig
127
125 291
For the examples, we’ll assume
that the edges in each vertex’s
adjacency list are sorted by
increasing edge cost.
Berlin
Potsdam
Magdeburg
53. ICSE: Introduction to Computer Sciences for Engineers – Lecture 09
ICSE
53
Graphs: Traversal and Search
Depth-First Traversal / Search from Berlin
149 125
Berlin
Magdeburg
Hamburg
Potsdam
Dresden
Hannover
203
3
5
156
112
127
151
Bremen
Leipzig
127
125 291
For the examples, we’ll assume
that the edges in each vertex’s
adjacency list are sorted by
increasing edge cost.
Berlin
Potsdam
Magdeburg
Leipzig
54. ICSE: Introduction to Computer Sciences for Engineers – Lecture 09
ICSE
54
Graphs: Traversal and Search
Depth-First Traversal / Search from Berlin
149 125
Berlin
Magdeburg
Hamburg
Potsdam
Dresden
Hannover
203
3
5
156
112
127
151
Bremen
Leipzig
127
125 291
For the examples, we’ll assume
that the edges in each vertex’s
adjacency list are sorted by
increasing edge cost.
Berlin
Potsdam
Magdeburg
Leipzig
Dresden
55. ICSE: Introduction to Computer Sciences for Engineers – Lecture 09
ICSE
55
Graphs: Traversal and Search
Depth-First Traversal / Search from Berlin
149 125
Berlin
Magdeburg
Hamburg
Potsdam
Dresden
Hannover
203
3
5
156
112
127
151
Bremen
Leipzig
127
125 291
For the examples, we’ll assume
that the edges in each vertex’s
adjacency list are sorted by
increasing edge cost.
Berlin
Potsdam
Magdeburg
Leipzig
Dresden
Hannover
56. ICSE: Introduction to Computer Sciences for Engineers – Lecture 09
ICSE
56
Graphs: Traversal and Search
Depth-First Traversal / Search from Berlin
149 125
Berlin
Magdeburg
Hamburg
Potsdam
Dresden
Hannover
203
3
5
156
112
127
151
Bremen
Leipzig
127
125 291
For the examples, we’ll assume
that the edges in each vertex’s
adjacency list are sorted by
increasing edge cost.
Berlin
Potsdam
Magdeburg
Leipzig
Dresden
Hannover
Bremen
57. ICSE: Introduction to Computer Sciences for Engineers – Lecture 09
ICSE
57
Graphs: Traversal and Search
Depth-First Traversal / Search from Berlin
149 125
Berlin
Magdeburg
Hamburg
Potsdam
Dresden
Hannover
203
3
5
156
112
127
151
Bremen
Leipzig
127
125 291
Berlin
Potsdam
Magdeburg
Leipzig
Dresden
Hannover
Bremen
Hamburg
For the examples, we’ll assume
that the edges in each vertex’s
adjacency list are sorted by
increasing edge cost.
58. ICSE: Introduction to Computer Sciences for Engineers – Lecture 09
ICSE
58
Graphs: Traversal and Search
Depth-First Traversal / Search from Magdeburg
149 125
Berlin
Magdeburg
Hamburg
Potsdam
Dresden
Hannover
203
3
5
156
112
127
151
Bremen
Leipzig
127
125 291
For the examples, we’ll assume
that the edges in each vertex’s
adjacency list are sorted by
increasing edge cost.
59. ICSE: Introduction to Computer Sciences for Engineers – Lecture 09
ICSE
59
Graphs: Traversal and Search
Depth-First Traversal / Search from Magdeburg
149 125
Berlin
Magdeburg
Hamburg
Potsdam
Dresden
Hannover
203
3
5
156
112
127
151
Bremen
Leipzig
127
125 291
Berlin
Potsdam
Magdeburg
Leipzig
Dresden
Hannover
Bremen
Hamburg
For the examples, we’ll assume
that the edges in each vertex’s
adjacency list are sorted by
increasing edge cost.
60. ICSE: Introduction to Computer Sciences for Engineers – Lecture 09
ICSE
60
Checking for Cycles in an Undirected Graph
• to discover a cycle in an undirected graph, we can:
• perform a Depth-First Traversal / Search, marking the vertices as visited
• when considering neighbors of a visited vertex,
if we discover one already marked as visited, there must be a cycle
• if no cycles found during the traversal, the graph is acyclic
• this doesn't work for directed graphs:
• c is a neighbor of both a and b
• there is no cycle
Graphs: Traversal and Search
d
e
f
i
h
c
b
a
c
b
a
61. ICSE: Introduction to Computer Sciences for Engineers – Lecture 09
ICSE
61
Breadth-First Traversal / Search
• visit a start vertex v, then explores the neighbor vertices first, before moving to
the next level:
• breadthFirstTrav(v)
• insert vertex v into a queue and mark it as visited
• while queue is not empty do
• remove vertex w of the queue
• visit vertex w
• for each vertex x in w’s neighbors do
• if ( vertex x has not been visited )
• insert vertex x into the queue and mark it as visited
• Breadth-First Traversal / Search
• breadthFirstTrav(1):
Graphs: Traversal and Search
1
6
2 8
5
3
4
7
62. ICSE: Introduction to Computer Sciences for Engineers – Lecture 09
ICSE
62
Breadth-First Traversal / Search
• visit a start vertex v, then explores the neighbor vertices first, before moving to
the next level:
• breadthFirstTrav(v)
• insert vertex v into a queue and mark it as visited
• while queue is not empty do
• remove vertex w of the queue
• visit vertex w
• for each vertex x in w’s neighbors do
• if ( vertex x has not been visited )
• insert vertex x into the queue and mark it as visited
• Breadth-First Traversal / Search
• breadthFirstTrav(1):
Graphs: Traversal and Search
1
6
2 8
5
3
4
7
63. ICSE: Introduction to Computer Sciences for Engineers – Lecture 09
ICSE
63
Breadth-First Traversal / Search
• visit a start vertex v, then explores the neighbor vertices first, before moving to
the next level:
• breadthFirstTrav(v)
• insert vertex v into a queue and mark it as visited
• while queue is not empty do
• remove vertex w of the queue
• visit vertex w
• for each vertex x in w’s neighbors do
• if ( vertex x has not been visited )
• insert vertex x into the queue and mark it as visited
• Breadth-First Traversal / Search
• breadthFirstTrav(1): 1
Graphs: Traversal and Search
1
6
2 8
5
3
4
7
64. ICSE: Introduction to Computer Sciences for Engineers – Lecture 09
ICSE
64
Breadth-First Traversal / Search
• visit a start vertex v, then explores the neighbor vertices first, before moving to
the next level:
• breadthFirstTrav(v)
• insert vertex v into a queue and mark it as visited
• while queue is not empty do
• remove vertex w of the queue
• visit vertex w
• for each vertex x in w’s neighbors do
• if ( vertex x has not been visited )
• insert vertex x into the queue and mark it as visited
• Breadth-First Traversal / Search
• breadthFirstTrav(1): 1
Graphs: Traversal and Search
1
6
2 8
5
3
4
7
65. ICSE: Introduction to Computer Sciences for Engineers – Lecture 09
ICSE
65
Breadth-First Traversal / Search
• visit a start vertex v, then explores the neighbor vertices first, before moving to
the next level:
• breadthFirstTrav(v)
• insert vertex v into a queue and mark it as visited
• while queue is not empty do
• remove vertex w of the queue
• visit vertex w
• for each vertex x in w’s neighbors do
• if ( vertex x has not been visited )
• insert vertex x into the queue and mark it as visited
• Breadth-First Traversal / Search
• breadthFirstTrav(1): 1 3
Graphs: Traversal and Search
1
6
2 8
5
3
4
7
66. ICSE: Introduction to Computer Sciences for Engineers – Lecture 09
ICSE
66
Breadth-First Traversal / Search
• visit a start vertex v, then explores the neighbor vertices first, before moving to
the next level:
• breadthFirstTrav(v)
• insert vertex v into a queue and mark it as visited
• while queue is not empty do
• remove vertex w of the queue
• visit vertex w
• for each vertex x in w’s neighbors do
• if ( vertex x has not been visited )
• insert vertex x into the queue and mark it as visited
• Breadth-First Traversal / Search
• breadthFirstTrav(1): 1 3
Graphs: Traversal and Search
1
6
2 8
5
3
4
7
67. ICSE: Introduction to Computer Sciences for Engineers – Lecture 09
ICSE
67
Breadth-First Traversal / Search
• visit a start vertex v, then explores the neighbor vertices first, before moving to
the next level:
• breadthFirstTrav(v)
• insert vertex v into a queue and mark it as visited
• while queue is not empty do
• remove vertex w of the queue
• visit vertex w
• for each vertex x in w’s neighbors do
• if ( vertex x has not been visited )
• insert vertex x into the queue and mark it as visited
• Breadth-First Traversal / Search
• breadthFirstTrav(1): 1 3 6
Graphs: Traversal and Search
1
6
2 8
5
3
4
7
68. ICSE: Introduction to Computer Sciences for Engineers – Lecture 09
ICSE
68
Breadth-First Traversal / Search
• visit a start vertex v, then explores the neighbor vertices first, before moving to
the next level:
• breadthFirstTrav(v)
• insert vertex v into a queue and mark it as visited
• while queue is not empty do
• remove vertex w of the queue
• visit vertex w
• for each vertex x in w’s neighbors do
• if ( vertex x has not been visited )
• insert vertex x into the queue and mark it as visited
• Breadth-First Traversal / Search
• breadthFirstTrav(1): 1 3 6
Graphs: Traversal and Search
1
6
2 8
5
3
4
7
69. ICSE: Introduction to Computer Sciences for Engineers – Lecture 09
ICSE
69
Breadth-First Traversal / Search
• visit a start vertex v, then explores the neighbor vertices first, before moving to
the next level:
• breadthFirstTrav(v)
• insert vertex v into a queue and mark it as visited
• while queue is not empty do
• remove vertex w of the queue
• visit vertex w
• for each vertex x in w’s neighbors do
• if ( vertex x has not been visited )
• insert vertex x into the queue and mark it as visited
• Breadth-First Traversal / Search
• breadthFirstTrav(1): 1 3 6 8
Graphs: Traversal and Search
1
6
2 8
5
3
4
7
70. ICSE: Introduction to Computer Sciences for Engineers – Lecture 09
ICSE
70
Breadth-First Traversal / Search
• visit a start vertex v, then explores the neighbor vertices first, before moving to
the next level:
• breadthFirstTrav(v)
• insert vertex v into a queue and mark it as visited
• while queue is not empty do
• remove vertex w of the queue
• visit vertex w
• for each vertex x in w’s neighbors do
• if ( vertex x has not been visited )
• insert vertex x into the queue and mark it as visited
• Breadth-First Traversal / Search
• breadthFirstTrav(1): 1 3 6 8
Graphs: Traversal and Search
1
6
2 8
5
3
4
7
71. ICSE: Introduction to Computer Sciences for Engineers – Lecture 09
ICSE
71
Breadth-First Traversal / Search
• visit a start vertex v, then explores the neighbor vertices first, before moving to
the next level:
• breadthFirstTrav(v)
• insert vertex v into a queue and mark it as visited
• while queue is not empty do
• remove vertex w of the queue
• visit vertex w
• for each vertex x in w’s neighbors do
• if ( vertex x has not been visited )
• insert vertex x into the queue and mark it as visited
• Breadth-First Traversal / Search
• breadthFirstTrav(1): 1 3 6 8 7
Graphs: Traversal and Search
1
6
2 8
5
3
4
7
72. ICSE: Introduction to Computer Sciences for Engineers – Lecture 09
ICSE
72
Breadth-First Traversal / Search
• visit a start vertex v, then explores the neighbor vertices first, before moving to
the next level:
• breadthFirstTrav(v)
• insert vertex v into a queue and mark it as visited
• while queue is not empty do
• remove vertex w of the queue
• visit vertex w
• for each vertex x in w’s neighbors do
• if ( vertex x has not been visited )
• insert vertex x into the queue and mark it as visited
• Breadth-First Traversal / Search
• breadthFirstTrav(1): 1 3 6 8 7 4
Graphs: Traversal and Search
1
6
2 8
5
3
4
7
73. ICSE: Introduction to Computer Sciences for Engineers – Lecture 09
ICSE
73
Breadth-First Traversal / Search
• visit a start vertex v, then explores the neighbor vertices first, before moving to
the next level:
• breadthFirstTrav(v)
• insert vertex v into a queue and mark it as visited
• while queue is not empty do
• remove vertex w of the queue
• visit vertex w
• for each vertex x in w’s neighbors do
• if ( vertex x has not been visited )
• insert vertex x into the queue and mark it as visited
• Breadth-First Traversal / Search
• breadthFirstTrav(1): 1 3 6 8 7 4 5
Graphs: Traversal and Search
1
6
2 8
5
3
4
7
74. ICSE: Introduction to Computer Sciences for Engineers – Lecture 09
ICSE
74
Breadth-First Traversal / Search
• visit a start vertex v, then explores the neighbor vertices first, before moving to
the next level:
• breadthFirstTrav(v)
• insert vertex v into a queue and mark it as visited
• while queue is not empty do
• remove vertex w of the queue
• visit vertex w
• for each vertex x in w’s neighbors do
• if ( vertex x has not been visited )
• insert vertex x into the queue and mark it as visited
• Breadth-First Traversal / Search
• breadthFirstTrav(1): 1 3 6 8 7 4 5 2
Graphs: Traversal and Search
1
6
2 8
5
3
4
7
75. ICSE: Introduction to Computer Sciences for Engineers – Lecture 09
ICSE
75
Graphs: Traversal and Search
Breadth-First Traversal / Search from Berlin
• Evolution of the queue:
125
149 125
Berlin
Magdeburg
Hamburg
Potsdam
Dresden
Hannover
203
3
5
156
112
127
151
Bremen
Leipzig
127
291
remove insert queue contents
Berlin Berlin
Berlin Potsdam, Magdeburg, Hamburg Potsdam, Magdeburg, Hamburg
Potsdam Dresden Magdeburg, Hamburg, Dresden
Magdeburg Leipzig, Hannover Hamburg, Dresden, Leipzig, Hannover
Hamburg Bremen Dresden, Leipzig, Hannover, Bremen
Dresden None Leipzig, Hannover, Bremen
Leipzig None Hannover, Bremen
Hannover None Bremen
Bremen None empty
76. ICSE: Introduction to Computer Sciences for Engineers – Lecture 09
ICSE
76
Graphs: Traversal and Search
Breadth-First Traversal / Search from Berlin
• Evolution of the queue:
125
149 125
Berlin
Magdeburg
Hamburg
Potsdam
Dresden
Hannover
203
3
5
156
112
127
151
Bremen
Leipzig
127
291
remove insert queue contents
Berlin Berlin
Berlin Potsdam, Magdeburg, Hamburg Potsdam, Magdeburg, Hamburg
Potsdam Dresden Magdeburg, Hamburg, Dresden
Magdeburg Leipzig, Hannover Hamburg, Dresden, Leipzig, Hannover
Hamburg Bremen Dresden, Leipzig, Hannover, Bremen
Dresden None Leipzig, Hannover, Bremen
Leipzig None Hannover, Bremen
Hannover None Bremen
Bremen None empty
77. ICSE: Introduction to Computer Sciences for Engineers – Lecture 09
ICSE
77
Graphs: Traversal and Search
Breadth-First Traversal / Search from Berlin
• Evolution of the queue:
125
149 125
Berlin
Magdeburg
Hamburg
Potsdam
Dresden
Hannover
203
3
5
156
112
127
151
Bremen
Leipzig
127
291
remove insert queue contents
Berlin Berlin
Berlin Potsdam, Magdeburg, Hamburg Potsdam, Magdeburg, Hamburg
Potsdam Dresden Magdeburg, Hamburg, Dresden
Magdeburg Leipzig, Hannover Hamburg, Dresden, Leipzig, Hannover
Hamburg Bremen Dresden, Leipzig, Hannover, Bremen
Dresden None Leipzig, Hannover, Bremen
Leipzig None Hannover, Bremen
Hannover None Bremen
Bremen None empty
78. ICSE: Introduction to Computer Sciences for Engineers – Lecture 09
ICSE
78
Graphs: Traversal and Search
Breadth-First Traversal / Search from Berlin
• Evolution of the queue:
125
149 125
Berlin
Magdeburg
Hamburg
Potsdam
Dresden
Hannover
203
3
5
156
112
127
151
Bremen
Leipzig
127
291
remove insert queue contents
Berlin Berlin
Berlin Potsdam, Magdeburg, Hamburg Potsdam, Magdeburg, Hamburg
Potsdam Dresden Magdeburg, Hamburg, Dresden
Magdeburg Leipzig, Hannover Hamburg, Dresden, Leipzig, Hannover
Hamburg Bremen Dresden, Leipzig, Hannover, Bremen
Dresden None Leipzig, Hannover, Bremen
Leipzig None Hannover, Bremen
Hannover None Bremen
Bremen None empty
79. ICSE: Introduction to Computer Sciences for Engineers – Lecture 09
ICSE
79
Graphs: Traversal and Search
Breadth-First Traversal / Search from Berlin
• Evolution of the queue:
125
149 125
Berlin
Magdeburg
Hamburg
Potsdam
Dresden
Hannover
203
3
5
156
112
127
151
Bremen
Leipzig
127
291
remove insert queue contents
Berlin Berlin
Berlin Potsdam, Magdeburg, Hamburg Potsdam, Magdeburg, Hamburg
Potsdam Dresden Magdeburg, Hamburg, Dresden
Magdeburg Leipzig, Hannover Hamburg, Dresden, Leipzig, Hannover
Hamburg Bremen Dresden, Leipzig, Hannover, Bremen
Dresden None Leipzig, Hannover, Bremen
Leipzig None Hannover, Bremen
Hannover None Bremen
Bremen None empty
80. ICSE: Introduction to Computer Sciences for Engineers – Lecture 09
ICSE
80
Graphs: Traversal and Search
Breadth-First Traversal / Search from Berlin
• Evolution of the queue:
125
149 125
Berlin
Magdeburg
Hamburg
Potsdam
Dresden
Hannover
203
3
5
156
112
127
151
Bremen
Leipzig
127
291
remove insert queue contents
Berlin Berlin
Berlin Potsdam, Magdeburg, Hamburg Potsdam, Magdeburg, Hamburg
Potsdam Dresden Magdeburg, Hamburg, Dresden
Magdeburg Leipzig, Hannover Hamburg, Dresden, Leipzig, Hannover
Hamburg Bremen Dresden, Leipzig, Hannover, Bremen
Dresden None Leipzig, Hannover, Bremen
Leipzig None Hannover, Bremen
Hannover None Bremen
Bremen None empty
81. ICSE: Introduction to Computer Sciences for Engineers – Lecture 09
ICSE
81
Graphs: Traversal and Search
Breadth-First Traversal / Search from Berlin
• Evolution of the queue:
125
149 125
Berlin
Magdeburg
Hamburg
Potsdam
Dresden
Hannover
203
3
5
156
112
127
151
Bremen
Leipzig
127
291
remove insert queue contents
Berlin Berlin
Berlin Potsdam, Magdeburg, Hamburg Potsdam, Magdeburg, Hamburg
Potsdam Dresden Magdeburg, Hamburg, Dresden
Magdeburg Leipzig, Hannover Hamburg, Dresden, Leipzig, Hannover
Hamburg Bremen Dresden, Leipzig, Hannover, Bremen
Dresden None Leipzig, Hannover, Bremen
Leipzig None Hannover, Bremen
Hannover None Bremen
Bremen None empty
82. ICSE: Introduction to Computer Sciences for Engineers – Lecture 09
ICSE
82
Graphs: Traversal and Search
Breadth-First Traversal / Search from Berlin
• Evolution of the queue:
125
149 125
Berlin
Magdeburg
Hamburg
Potsdam
Dresden
Hannover
203
3
5
156
112
127
151
Bremen
Leipzig
127
291
remove insert queue contents
Berlin Berlin
Berlin Potsdam, Magdeburg, Hamburg Potsdam, Magdeburg, Hamburg
Potsdam Dresden Magdeburg, Hamburg, Dresden
Magdeburg Leipzig, Hannover Hamburg, Dresden, Leipzig, Hannover
Hamburg Bremen Dresden, Leipzig, Hannover, Bremen
Dresden None Leipzig, Hannover, Bremen
Leipzig None Hannover, Bremen
Hannover None Bremen
Bremen None empty
83. ICSE: Introduction to Computer Sciences for Engineers – Lecture 09
ICSE
83
Graphs: Traversal and Search
Breadth-First Traversal / Search from Berlin
• Evolution of the queue:
125
149 125
Berlin
Magdeburg
Hamburg
Potsdam
Dresden
Hannover
203
3
5
156
112
127
151
Bremen
Leipzig
127
291
remove insert queue contents
Berlin Berlin
Berlin Potsdam, Magdeburg, Hamburg Potsdam, Magdeburg, Hamburg
Potsdam Dresden Magdeburg, Hamburg, Dresden
Magdeburg Leipzig, Hannover Hamburg, Dresden, Leipzig, Hannover
Hamburg Bremen Dresden, Leipzig, Hannover, Bremen
Dresden None Leipzig, Hannover, Bremen
Leipzig None Hannover, Bremen
Hannover None Bremen
Bremen None empty
84. ICSE: Introduction to Computer Sciences for Engineers – Lecture 09
ICSE
84
Graphs: Traversal and Search
Breadth-First Traversal / Search from Berlin
Berlin
Potsdam
Magdeburg
Leipzig
Dresden
Hannover
Bremen
Hamburg
Berlin
Potsdam Magdeburg
Leipzig
Dresden Hannover Bremen
Hamburg
125
149 125
Berlin
Magdeburg
Hamburg
Potsdam
Dresden
Hannover
203
3
5
156
112
127
151
Bremen
Leipzig
127
291
breadth-first spanning tree:
depth-first spanning tree:
85. ICSE: Introduction to Computer Sciences for Engineers – Lecture 09
ICSE
85
Graphs: Traversal and Search
Breadth-First Traversal / Search from Magdeburg
Berlin
Potsdam
Magdeburg
Leipzig
Dresden
Hannover
Bremen Hamburg
125
149 125
Berlin
Magdeburg
Hamburg
Potsdam
Dresden
Hannover
203
3
5
156
112
127
151
Bremen
Leipzig
127
291
breadth-first spanning tree:
86. ICSE: Introduction to Computer Sciences for Engineers – Lecture 09
ICSE
86
Summary
Graphs
• Types and Properties
• Representation
• Traversal and Search
87. ICSE: Introduction to Computer Sciences for Engineers – Lecture 09
ICSE
87
Recommended Reading
Chapter 14: Graphs
• 14.1 Graphs
• 14.2 Data Structures for Graphs
• 14.3 Graph Traversal
Data Structures &
Algorithms in Java
by Michael T. Goodrich,
Roberto Tamassia and
Michael H. Goldwasser
88. ICSE: Introduction to Computer Sciences for Engineers – Lecture 09
ICSE
88
Coming up…
Graph Algorithms
• Topological Ordering
• Topological Sort Algorithm
• The Lowest-Cost Problem
• Prim-Jarnik Algorithm
• The Shortest-Path Problem
• Dijkstra‘s Algorithm
Algorithm Patterns
• The Knapsack Problem
• Greedy strategy
• Dynamic programming