Upload
georgiana-warren
View
215
Download
0
Embed Size (px)
Citation preview
GraphsRepresenting and Using Graphs
Graphs
What is a graph?
A graph is an abstract data structure used to represent a set of objects, some of which are connected by links.
A
D
EC
B
F
Graphs
Key Terminology
Vertex: The objects that a graph is composed of. e.g. A, B. Alternate name: Node
Edge: The links between the vertices. Alternate name: Arc Degree: The number of edges connected to a vertex. e.g. A
has degree 3, B degree 2. Neighbour: Vertex X is a neighbour of vertex Y if there is an
edge that directly links X and Y. Path: A sequence of edges leading from one vertex to
another.
A
D
EC
B
Graphs
Directed and Undirected
Graphs can be directed or undirected.
A
C
B
A
C
B
D
Directed Graph (Digraph): A graph in which a direction is associated with each edge between a pair of vertices.
Undirected Graph: A graph in which no direction is associated with the edges.
Graphs
Weighted Graphs
Sometimes a value is associated with each edge in a graph. These values are known as weights and this type of graph is known as a weighted graph or labelled graph.
A
D
E
B
C
20
7
15
12
5
Graphs
Trees A tree is a special type of graph which is
undirected, connected and has no cycles:
A rooted tree has one vertex designated as the root which is usually drawn at the top.
DA
CB
CB
DA
DC
BA
DC
B
A Not a tree as contains a cycle.
Not a tree as not connected.
Not a tree as directed.
Connected, undirected, no cycles: A TREE
Graphs
Practical Applications of Graphs
Generally want to find least distance / minimal cost.
Application Vertices Edges
Modelling the hyperlink structure of a website.
Web pages Hyperlinks
Map of road network used for route planning.
Locations Roads
Social analysis e.g. the relationships between people.
People Friendships
Mapping breeding/migration patterns in biology.
Locations Migration routes
Planning component layout on microchips to minimise chip size.
Components Wires
Computer network traffic routing. Routers Comms Lines
Map of rail network used for fare calculation.
Stations Railway Lines
Mapping a maze. Junctions Passageways
Graphs
Representing Graphs
Graphs can be represented in a programming language as a data structure.
The two standard methods of representing a graph are:
o Adjacency List
o Adjacency Matrix
Graphs
Adjacency Matrix
A 2-dimensional array (matrix) is used to represent the graph.
The array indices in each dimension run from 1 to n, where n is the number of vertices in the graph.
The value stored at position [ i , j ] is a count of how many edges there are that directly connect vertices i and j.
For a simple graph (undirected, no loops, no more than one edge between two vertices) this value will always be 0 or 1.
Graphs
Example Adjacency Matrices
1 2 3 4 5
1 0 1 1 1 1
2 1 0 0 0 0
3 1 0 0 0 0
4 1 0 0 0 1
5 1 0 0 1 0
1
4
5
2
3
1
4
5
2
3
1 2 3 4 5
1 0 1 1 0 1
2 0 0 0 0 0
3 0 0 0 0 0
4 1 0 0 0 1
5 0 0 0 0 0
Undirected Graph
Directed Graph
Graphs
Adjacency List
For each vertex, a list is maintained of the adjacent vertices, i.e. those vertices that are directly connected to the vertex by an edge.
Adjacent vertices are also known as neighbours.
The adjacency list could be represented as a linked list.
Graphs
Example Adjacency Lists
Vertex
Adjacent
1 2,3,4,5
2 1
3 1
4 1,5
5 1,4
1
4
5
2
3
1
4
5
2
3
Vertex
Adjacent
1 2,3,5
2
3
4 1,5
5
Undirected Graph
Directed Graph
Graphs
Are Lists or Matrices Better? In determining whether a list or matrix is
better, two factors need to be considered:oHow much storage space the data structure will
require.oHow quickly the data structure can be modified or
checked to e.g. Add a new edge or test if an edge exists between two vertices.
The best option depends upon how the list will be used.
Graphs
Are Lists or Matrices Better? An adjacency matrix is more appropriate
when:o there are many edges between vertices,owhen edges may be frequently changed,owhen presence/absence of specific edges needs
to be tested frequently. Adjacency list most appropriate when:o there are few edges between vertices (when
graph is sparse),owhen edges change infrequently,owhen presence/absence of specific edges does
not need to be tested frequently.
Graphs
Are Lists or Matrices Better? A commonly held misconception is that the number of
vertices in a graph determines whether a list or matrix is most appropriate because an adjacency matrix would take up too much storage space for a large number of vertices.
This is not true!o A graph with a large number of vertices and a small number of
edges could be represented in less storage space using an adjacency list as this would avoid storing lots of 0s indicating no edges that would be required in the equivalent matrix.
o A graph with a large number of vertices and a large number of edges could be represented in less storage space using an adjacency matrix as many edges would need to be represented and the matrix would avoid the overhead of storing pointers and node numbers that a list would require.
So, the influencing factor with regard to storage space is always the number of edges even for large numbers of vertices.
Graphs
Searching Graphs
A commonly used operation on a graph is to search for an item within the graph.
To do this a systematic way of exploring each vertex (without getting stuck) is required.
Two common methods of doing this are a depth first search and a breadth first search.
We will look at examples of these methods on the next few slides.
Each example explores the entire graph to illustrate the search order. In reality, for efficiency, an algorithm would terminate as soon as the item that was being searched for was found.
Graphs
Depth First Search
Philosophy: oTravel as far into a graph as possible,
backtracking to vertices which haven’t been explored yet only when a ‘dead end’ has been reached.
Process:oChoose a vertex to start from (usually the root
vertex if the graph is a rooted tree).oExplore as far as possible along each edge until a
vertex is reached from which it is no longer possible to follow an unexplored edge.
oBacktrack to the most recently visited node that hasn’t yet been fully explored.
oRepeat the process.
Graphs
Depth First Search (Tree)
This animation shows a depth first search of a rooted tree.
The search starts at the root node (1).
It is simpler to search a tree than a graph as there are no loops.
Depth first searching is often implemented using recursion.
1
3
6
7
2
54
Graphs
Depth First Search (Graph) A graph that is not a
tree may contain loops (3453...) so algorithm must avoid getting trapped in these when searching.
Avoid this by using two flags to mark nodes as:o Discoveredo Completely Explored
Backtrack when a discovered node is encountered.
Watch the animation to see a search progress.
1
3
6
7
2
54
1
22
33
44 55
66
771
Discovered Completely Explored
Graphs
Breadth First Search
Philosophy:o Look at the vertices that are closest to the starting vertex
first, exploring all of the neighbours of a vertex before going deeper into the graph.
Process:o Choose a vertex to start from.o Visit all of the vertices that are neighbours of this vertex
(and have not already been discovered).o Mark each vertex as discovered as soon as it is visited.o Then visit all of the vertices that are neighbours of this
first set of neighbours and so on. A queue can be used to keep track of vertices
that have been visited which still have neighbours that haven’t been explored yet.
Graphs
66
11 21 2 31 2 3 42 3 43 43 4 53 4 5 6
Breadth First Search (Tree/Graph)
1
37
42
5
8
9
4 5 6
11
224
3
4
6
77 88
99
3
5
4
5
This animation shows a breadth first search.
The queue stores the discovered nodes that are waiting to be visited.
The algorithm tracks discovered nodes so works for graphs as well as trees.
4 5 6 74 5 6 7 85 6 7 86 7 87 888 99
Queue
Vertex currentlybeing processed
Discovered Currently Processing
Graphs
Shortest Path
Another common graphing problem is to find the shortest path between two vertices.
A shortest path is usually looked for in a weighted graph, where a cost is associated with traversing each edge.
For example, in a satellite navigation device, a road network might be represented by a graph where each vertex is a road junction and each edge is a road.
A time is associated with traversing each edge (road). The optimal route between two locations would be the
path which produced the lowest total time when the times associated with each edge on the route are added up.
Graphs
Dijkstra’s Algorithm
Dijkstra’s algorithm can be used to find the shortest path from a vertex to any other vertex.
The algorithm tries out every possible route. Sadly, a simple implementation of Dijkstra’s
algorithm has time complexity O(v2) where v is the number of vertices in the graph.
More sophisticated implementations are more time efficient but for large graphs, such as a map, it is not feasible to use Dijkstra’s algorithm.
Graphs
More Efficient Shortest Path Therefore, a more sophisticated algorithm such
as the A* algorithm must be used for large graphs like a satnav map.
Unlike Djkstra’s algorithm, the A* algorithm does not search the entire graph to find the shortest route, it uses a heuristic to disregard vertices which are unlikely to be of interest because they seem to lie in the wrong direction.
The A* algorithm is therefore far more time efficient than Dijkstra’s algorithm.
Graphs
Other Graph Problems to Explore
Euler Circuit Problem: Determine if a graph has an Euler circuit, i.e. a path from a vertex back to itself that traverses each edge exactly once.
Travelling Salesman: Given a list of cities and the distances between them, find the shortest possible tour that visits each city exactly once.
Graphs
End of Presentati
on
Graphs
Trees or Not Solutions
DC
B
A
CB
DA
DA
CB
DC
BA
Not a tree as not connected. Not a tree as contains circuit.
Not a tree as contains circuit. A tree.
Q1)
Graphs
Adjacency Matrix Solution
Q2) Adjacency matrix for graph:
A
D
EC
B
F
A B C D E F
A 0 1 0 1 1 0
B 1 0 1 0 1 0
C 0 1 0 0 0 0
D 1 0 0 0 1 0
E 1 1 0 1 0 0
F 0 0 0 0 0 0
Graphs
Adjacency List Solution
Vertex
Adjacent
1 2,4
2 3
3
4 1,5
5 2,4
1
4
2
3
5
Q3) One possible drawing of the graph:
Graphs
Matrix or List Solution
Q4) An adjacency matrix is more appropriate when:
there are many edges between vertices,
when edges may be frequently changed,
when presence/absence of specific edges needs to be tested frequently.
Graphs
Depth First Search (Tree) Solution
1
4
32
65
Q5) Search Order:
1 2 4 5 6 7 8 3
87
Graphs
Depth First Search (Graph) Solution
Q6) Search Order:
1 2 5 6 7 4 3
1
5
42
76
3
Graphs
Breadth First Search (Tree) Solution
Q7) Search Order:
1 2 4 3 5 6 7 8
1
4
32
65
87
Graphs
Weighted Graphs Solution
Q8) Shortest Path:
C D A E B Length 10 + 12 + 7 + 10 = 39
A
D
E
B
C
20
7
20
12
2510
10