Presentation is loading. Please wait.

Presentation is loading. Please wait.

Representing and Using Graphs

Similar presentations


Presentation on theme: "Representing and Using Graphs"— Presentation transcript:

1 Representing and Using Graphs

2 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. B A C E D F

3 Key Terminology A D E C B 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.

4 Directed and Undirected
Graphs can be directed or undirected. Undirected Graph: A graph in which no direction is associated with the edges. Directed Graph (Digraph): A graph in which a direction is associated with each edge between a pair of vertices. A C B A C B D Note that where there are two edges in opposing directions between the same pair of vertices e.g. C to D and D to C in a directed graph, these are often drawn as a double-headed arrow.

5 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

6 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. A A Not a tree as contains a cycle. Connected, undirected, no cycles: A TREE D B D C C B D C B A B Not a tree as directed. Not a tree as not connected. C D A

7 Practical Applications of Graphs
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. 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 Generally want to find least distance / minimal cost.

8 Representing Graphs Graphs can be represented in a programming language as a data structure. The two standard methods of representing a graph are: Adjacency List Adjacency Matrix

9 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.

10 Example Adjacency Matrices
Undirected Graph Directed Graph 1 4 5 2 3 1 4 5 2 3 1 2 3 4 5 1 2 3 4 5 Matrices for undirected graphs are symmetric.

11 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.

12 Example Adjacency Lists
Undirected Graph Directed Graph 1 4 5 2 3 1 4 5 2 3 Vertex Adjacent 1 2,3,4,5 2 3 4 1,5 5 1,4 Vertex Adjacent 1 2,3,5 2 3 4 1,5 5 Matrices for undirected graphs are symmetric.

13 Are Lists or Matrices Better?
In determining whether a list or matrix is better, two factors need to be considered: How much storage space the data structure will require. How 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.

14 Are Lists or Matrices Better?
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. Adjacency list most appropriate when: there are few edges between vertices (when graph is sparse), when edges change infrequently, when presence/absence of specific edges does not need to be tested frequently.

15 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! 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. 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. This slide is included because lots of students made this error in the AQA COMP3 exam in June 2010.

16 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.

17 Depth First Search Philosophy: Process:
Travel 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: Choose a vertex to start from (usually the root vertex if the graph is a rooted tree). Explore as far as possible along each edge until a vertex is reached from which it is no longer possible to follow an unexplored edge. Backtrack to the most recently visited node that hasn’t yet been fully explored. Repeat the process. The concepts of pre post and in-order traversals are included on the A Level spec but are not covered in this presentation. They have been on the specification for many years in the context of trees, so are an area that delegates are likely to be familiar with already.

18 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. 7 1 2 6 3 5 4

19 Depth First Search (Graph)
A graph that is not a tree may contain loops (3453...) so algorithm must avoid getting trapped in these when searching. Avoid this by using two flags to mark nodes as: Discovered Completely Explored Backtrack when a discovered node is encountered. Watch the animation to see a search progress. 7 7 7 1 1 1 2 2 2 6 6 6 3 3 3 5 5 5 4 4 4 Discovered Completely Explored

20 Breadth First Search Philosophy: Process:
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: Choose a vertex to start from. Visit all of the vertices that are neighbours of this vertex (and have not already been discovered). Mark each vertex as discovered as soon as it is visited. 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.

21 Breadth First Search (Tree/Graph)
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. 1 1 1 4 4 4 1 4 2 2 2 3 3 3 7 7 7 8 8 8 Vertex currently being processed Could comment on advantages/disadvantages of breadth and depth first searching and also when they might be used. 6 6 6 5 5 5 9 9 9 Queue 8 7 8 6 7 8 8 9 9 2 3 4 4 5 6 3 4 5 6 5 6 7 8 3 4 3 4 5 1 2 4 5 6 7 8 1 4 5 6 7 1 2 3 4 1 2 3 Discovered Currently Processing

22 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.

23 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.

24 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.

25 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.

26 End of Presentation

27 Trees or Not Solutions Q1) C B D A D C B A
Not a tree as not connected. Not a tree as contains circuit. D C B A D A C B Not a tree as contains circuit. A tree.

28 Adjacency Matrix Solution
Q2) Adjacency matrix for graph: A B C D E F 1 A D E C B F

29 Adjacency List Solution
Q3) One possible drawing of the graph: 1 4 2 3 5 Vertex Adjacent 1 2,4 2 3 4 1,5 5

30 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.

31 Depth First Search (Tree) Solution
Q5) Search Order: 1 2 4 5 6 7 8 3 1 2 3 4 6 5 8 7

32 Depth First Search (Graph) Solution
Q6) Search Order: 1 2 5 6 7 4 3 1 5 4 2 7 6 3

33 Breadth First Search (Tree) Solution
Q7) Search Order: 1 2 4 3 5 6 7 8 1 2 3 4 6 5 8 7

34 Weighted Graphs Solution
Q8) Shortest Path: C  D  A  E  B Length = 39 20 B A 25 7 10 C E 12 D 20 10


Download ppt "Representing and Using Graphs"

Similar presentations


Ads by Google