Representing and Using Graphs

Slides:



Advertisements
Similar presentations
Graphs CSC 220 Data Structure. Introduction One of the Most versatile data structures like trees. Terminology –Nodes in trees are vertices in graphs.
Advertisements

IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture10.
CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
Graphs Chapter 12. Chapter Objectives  To become familiar with graph terminology and the different types of graphs  To study a Graph ADT and different.
© 2006 Pearson Addison-Wesley. All rights reserved14 A-1 Chapter 14 excerpts Graphs (breadth-first-search)
CS 206 Introduction to Computer Science II 11 / 07 / 2008 Instructor: Michael Eckmann.
Graphs Graphs are the most general data structures we will study in this course. A graph is a more general version of connected nodes than the tree. Both.
CS 206 Introduction to Computer Science II 11 / 11 / Veterans Day Instructor: Michael Eckmann.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 21: Graphs.
Graph & BFS.
Graphs CS-240/341. Graphs Used for representing many-to-many relationships –can take two forms directed (digraph) - a finite set of elements called vertices.
© 2006 Pearson Addison-Wesley. All rights reserved14 A-1 Chapter 14 Graphs.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
Graphs CS-240/341. Uses for Graphs computer networks and routing airline flights geographic maps course prerequisite structures tasks for completing a.
Graph COMP171 Fall Graph / Slide 2 Graphs * Extremely useful tool in modeling problems * Consist of: n Vertices n Edges D E A C F B Vertex Edge.
Lecture 14: Graph Algorithms Shang-Hua Teng. Undirected Graphs A graph G = (V, E) –V: vertices –E : edges, unordered pairs of vertices from V  V –(u,v)
Graph & BFS Lecture 22 COMP171 Fall Graph & BFS / Slide 2 Graphs * Extremely useful tool in modeling problems * Consist of: n Vertices n Edges D.
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
CS 206 Introduction to Computer Science II 11 / 03 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 03 / 25 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 03 / 30 / 2009 Instructor: Michael Eckmann.
Review of Graphs A graph is composed of edges E and vertices V that link the nodes together. A graph G is often denoted G=(V,E) where V is the set of vertices.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
Chapter 14 Graphs. © 2004 Pearson Addison-Wesley. All rights reserved Terminology G = {V, E} A graph G consists of two sets –A set V of vertices,
COSC 2007 Data Structures II Chapter 14 Graphs III.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
Common final examinations When: Wednesday, 12/11, 3:30-5:30 PM Where: Ritter Hall - Walk Auditorium 131 CIS 1166 final When: Wednesday, 12/11, 5:45-7:45.
Graphs. Made up of vertices and arcs Digraph (directed graph) –All arcs have arrows that give direction –You can only traverse the graph in the direction.
Graphs. Definitions A graph is two sets. A graph is two sets. –A set of nodes or vertices V –A set of edges E Edges connect nodes. Edges connect nodes.
10 Copyright © William C. Cheng Data Structures - CSCI 102 Graph Terminology A graph consists of a set of Vertices and a set of Edges C A B D a c b d e.
Most of contents are provided by the website Graph Essentials TJTSD66: Advanced Topics in Social Media.
Graphs. Graphs Similar to the graphs you’ve known since the 5 th grade: line graphs, bar graphs, etc., but more general. Those mathematical graphs are.
Graphs A graphs is an abstract representation of a set of objects, called vertices or nodes, where some pairs of the objects are connected by links, called.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 13: Graphs Data Abstraction & Problem Solving with C++
1 Directed Graphs Chapter 8. 2 Objectives You will be able to: Say what a directed graph is. Describe two ways to represent a directed graph: Adjacency.
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
© 2006 Pearson Addison-Wesley. All rights reserved 14 A-1 Chapter 14 Graphs.
Graphs Upon completion you will be able to:
Graph. Graph Usage I want to visit all the known famous places starting from Seoul ending in Seoul Knowledge: distances, costs Find the optimal(distance.
Graphs & Paths Presentation : Part II. Graph representation Given graph G = (V, E). May be either directed or undirected. Two common ways to represent.
Chapter 20: Graphs. Objectives In this chapter, you will: – Learn about graphs – Become familiar with the basic terminology of graph theory – Discover.
Graphs. Graph Definitions A graph G is denoted by G = (V, E) where  V is the set of vertices or nodes of the graph  E is the set of edges or arcs connecting.
Graphs + Shortest Paths David Kauchak cs302 Spring 2013.
Graph Representations And Traversals. Graphs Graph : – Set of Vertices (Nodes) – Set of Edges connecting vertices (u, v) : edge connecting Origin: u Destination:
Data Structures and Algorithm Analysis Graph Algorithms Lecturer: Jing Liu Homepage:
1 GRAPHS – Definitions A graph G = (V, E) consists of –a set of vertices, V, and –a set of edges, E, where each edge is a pair (v,w) s.t. v,w  V Vertices.
CSE 373: Data Structures and Algorithms Lecture 21: Graphs V 1.
Code: BCA302 Data Structures with C Prof. (Dr.) Monalisa Banerjee By.
Data Structures & Algorithm Analysis lec(8):Graph T. Souad alonazi
CS 201: Design and Analysis of Algorithms
Graphs Chapter 20.
Graphs Representation, BFS, DFS
CSC317 Graph algorithms Why bother?
Common final examinations
CS 3343: Analysis of Algorithms
I206: Lecture 15: Graphs Marti Hearst Spring 2012.
CS120 Graphs.
Graph.
Graphs Representation, BFS, DFS
Graphs & Graph Algorithms 2
Graph Theory.
Graphs Chapter 13.
Graphs Chapter 15 explain graph-based algorithms Graph definitions
Graphs Chapter 11 Objectives Upon completion you will be able to:
Graphs.
Chapter 11 Graphs.
Chapter 15 Graphs © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Chapter 14 Graphs © 2011 Pearson Addison-Wesley. All rights reserved.
INTRODUCTION A graph G=(V,E) consists of a finite non empty set of vertices V , and a finite set of edges E which connect pairs of vertices .
Presentation transcript:

Representing and Using 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. B A C E D F

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.

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.

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

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

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.

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

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.

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.

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.

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.

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.

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.

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.

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.

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.

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

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

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.

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

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.

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.

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.

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.

End of Presentation

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.

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

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

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.

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

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

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

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