Graphs A ‘Graph’ is a diagram that shows how things are connected together. It makes no attempt to draw actual paths or routes and scale is generally inconsequential.

Slides:



Advertisements
Similar presentations
Decision Maths Networks Kruskals Algorithm Wiltshire Networks A Network is a weighted graph, which just means there is a number associated with each.
Advertisements

Lecture 15. Graph Algorithms
Decision Maths Dijkstra’s Algorithm.
Graph Theory Arnold Mesa. Basic Concepts n A graph G = (V,E) is defined by a set of vertices and edges v3 v1 v2Vertex (v1) Edge (e1) A Graph with 3 vertices.
Networks Prim’s Algorithm
22C:19 Discrete Math Graphs Fall 2014 Sukumar Ghosh.
Midwestern State University Department of Computer Science Dr. Ranette Halverson CMPS 2433 – CHAPTER 4 GRAPHS 1.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture10.
Chapter 15 Graph Theory © 2008 Pearson Addison-Wesley. All rights reserved.
Aims: To know the terms: tree, spanning tree, minimum spanning tree. To understand that a minimum spanning tree connects a network using the lowest possible.
Decision Maths Graphs Wiltshire Graphs A graph is just a diagram made up of “dots” and “lines”. These are all graphs. The dots are called “nodes” or.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 21: Graphs.
3 -1 Chapter 3 The Greedy Method 3 -2 The greedy method Suppose that a problem can be solved by a sequence of decisions. The greedy method has that each.
Greedy Algorithms Reading Material: Chapter 8 (Except Section 8.5)
Spring 2010CS 2251 Graphs Chapter 10. Spring 2010CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs.
Graphs & Graph Algorithms 2 Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
Greedy Algorithms Like dynamic programming algorithms, greedy algorithms are usually designed to solve optimization problems Unlike dynamic programming.
Algorithms on graphs In Decision Mathematics, a graph consists of points (called vertices or nodes) which are connected by lines (edges or arcs). Eg in.
Chapter 15 Graph Theory © 2008 Pearson Addison-Wesley. All rights reserved.
C o n f i d e n t i a l HOME NEXT Subject Name: Data Structure Using C Unit Title: Graphs.
1 Shortest Path Calculations in Graphs Prof. S. M. Lee Department of Computer Science.
Data Structures and Algorithms Graphs Minimum Spanning Tree PLSD210.
© The McGraw-Hill Companies, Inc., Chapter 3 The Greedy Method.
Shortest Path Algorithm This is called “Dijkstra’s Algorithm” …pronounced “Dirk-stra”
Shortest Path. Dijkstra’s Algorithm finds the shortest path from the start vertex to every other vertex in the network. We will find the shortest path.
Programming for Geographical Information Analysis: Advanced Skills Online mini-lecture: Introduction to Networks Dr Andy Evans.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
Minimum spanning trees Aims: To know the terms: tree, spanning tree, minimum spanning tree. To understand that a minimum spanning tree connects a network.
Chapter 2 Graph Algorithms.
Networks.
COSC 2007 Data Structures II Chapter 14 Graphs III.
Which of these can be drawn without taking your pencil off the paper and without going over the same line twice? If we can find a path that goes over all.
Can you connect the dots as shown without taking your pen off the page or drawing the same line twice.
TCP Traffic and Congestion Control in ATM Networks
Minimum spanning trees Aims: To know the terms: tree, spanning tree, minimum spanning tree. To understand that a minimum spanning tree connects a network.
Minimum Spanning Trees Prof. Sin-Min Lee Dept. of Computer Science, San Jose State University.
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.
Fundamental Data Structures and Algorithms (Spring ’05) Recitation Notes: Graphs Slides prepared by Uri Dekel, Based on recitation.
Basic Notions on Graphs. The House-and-Utilities Problem.
1 12/2/2015 MATH 224 – Discrete Mathematics Formally a graph is just a collection of unordered or ordered pairs, where for example, if {a,b} G if a, b.
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 Part II Lecture 7. Lecture Objectives  Topological Sort  Spanning Tree  Minimum Spanning Tree  Shortest Path.
Minimum Spanning tree Prim’s algorithm 1.Select any vertex 2.Select the shortest edge connected to that vertex 3.Select the shortest edge which connects.
Chapter 20: Graphs. Objectives In this chapter, you will: – Learn about graphs – Become familiar with the basic terminology of graph theory – Discover.
DECISION 1. How do you do a Bubble Sort? Bubble Sort:  You compare adjacent items in a list;  If they are in order, leave them.  If they are not in.
Spanning Trees Dijkstra (Unit 10) SOL: DM.2 Classwork worksheet Homework (day 70) Worksheet Quiz next block.
Grade 11 AP Mathematics Graph Theory Definition: A graph, G, is a set of vertices v(G) = {v 1, v 2, v 3, …, v n } and edges e(G) = {v i v j where 1 ≤ i,
1 Data Structures and Algorithms Graphs. 2 Graphs Basic Definitions Paths and Cycles Connectivity Other Properties Representation Examples of Graph Algorithms:
Networks Teacher’s guide. Networks 2 Problem 1 – the Link The EMTV Company needs to install a new cable system that links the six major towns in its.
1 1 Slide © 2005 Thomson/South-Western Chapter 9 Network Models n Shortest-Route Problem n Minimal Spanning Tree Problem n Maximal Flow Problem.
Redraw these graphs so that none of the line intersect except at the vertices B C D E F G H.
Graph theory Definitions Trees, cycles, directed graphs.
Planar Graphs & Euler’s Formula
I206: Lecture 15: Graphs Marti Hearst Spring 2012.
Graph Algorithm.
Spanning Trees.
Graphs Chapter 13.
Decision Maths Dijkstra’s Algorithm.
Shortest Path.
Shortest Path.
Chapter 11 Graphs.
Minimum spanning trees
Minimum spanning trees
Decision Maths Graphs.
Minimum spanning trees
Networks Kruskal’s Algorithm
Minimum Spanning tree Select any vertex
Shortest Path.
Networks Prim’s Algorithm
Presentation transcript:

Graphs A ‘Graph’ is a diagram that shows how things are connected together. It makes no attempt to draw actual paths or routes and scale is generally inconsequential. If ‘weights’ are attached to the paths (to describe flows, times or distances) then the graph is called a ‘Network’.

Graph Vocabulary AB CD A graph is made up of nodes (dots) and arcs (lines/curves). It can contain multiple arcs and loops. A graph without any loops or multiple arcs is called simple. In a graph arcs are generally ‘two way’. A graph with at least one directed arc is called a di-graph. A subgraph is a small part of a graph, a subset of arcs and nodes AB CD AB D

Since graphs are only used to describe how thing are connected together, the lengths and positions of the arcs are not important. This means that graphs relaying the same information can be drawn in many different ways. We call two graphs that are really the same Isomorphic. Which of these graphs are isomorphic? How can you easily tell?

We can also try and capture the information in the Graph using a matrix. When written in this form it is much easier to spot if two graphs are isomorphic Clearly it can be difficult to tell if two graphs are isomorphic. To make life easier we attempt to draw graphs so that none of the arcs cross, this isn’t always possible, but graphs for which it is possible are called planar. This graph is planar because we can draw it like this with no arcs crossing A B C D ABCDABCD ABCDABCD A B C D A B C D

1) Look at the diagram for a circuit board opposite. Draw a graph to show how the six components are ‘directly’ connected to each other. 2) Represent the graphs below with a matrix. 3) Decide which of the graphs below are planar A B C D E A B C D E A B C D A B D C What do you notice about the matrices for i), ii) and iv) compared to iii)? Why is this the case? i) ii)iii) iv) i) ii)iii) iv)

AB CD A complete graph is one where every node is directly connected to every other node. This is the graph of K 4. A B C D F G E A bi-partite graph is a graph where the nodes split into 2 sets so that nodes in each set are not connected to other nodes in the same set. Types of Graphs and Networks

AB CD A walk/route around a graph/network is a sequence of linked arcs Eg. A – B – D – B – D – C A walk/route is closed if it starts and ends at the same node Eg. A – B – D – B – A A trail is a walk/route that doesn’t visit an edge more than once Eg. A – B – D – B (along alternate arc) A path is a trail that doesn’t visit a node more than once Eg. A – B – D A cycle is a path with the extra condition that the start and end nodes are the same Eg. B – D – B A Hamiltonian cycle is a cycle that visits every node (ie it will visit every node exactly once apart from the start/end node) Eg. A – B – C - D A graph/network is connected if every pair of nodes can be linked by a trail. The order/degree/valency of a node is the number of arcs that are incident to it Eg The order of A is 2, the order of B is 3, the order of C is 4

1)Is the complete graph with 6 nodes planar? 2) Look at the map opposite. Use the map opposite to determine the shortest distance between any two towns. Use this information to draw a graph representing the shortest distance between each of the towns. 3) Which of the graphs below contain a Hamiltonian cycle? A B C D E F A B C D E F A B C D E F G H A B C D E F G

Lesson Objective: Trees, Spanning Trees and Minimum Spanning Trees

Look at these graphs. They are all ‘trees’. What do you think makes a graph a tree? Can you draw some ‘trees’ of your own?

A tree with is a connected graph that contains only trivial cycles. Draw 2 ‘different trees’ with 4 nodes Draw 2 ‘different trees’ with 5 nodes Draw 2 ‘different trees’ with 6 nodes What can you say about the number of arcs in a tree with ‘n’ nodes. Why? How many ‘different’ trees are there with 3 nodes? How many ‘different’ trees are there with 4 nodes? 5 nodes? Etc.

When you find a tree for a network it is often called a spanning tree because it is a way of linking the nodes of the network together using the smallest number of arcs. Find two ‘different’ spanning trees for the network below? Add up the weights of the arcs in your spanning trees are they the same? How many different spanning trees can you find? Which has the smallest total weight? The spanning tree with the smallest total weight for a particular network is called the minimum spanning tree A B C D E F G 9 4 6

Imagine the dots on this page represent underground stations. The stations need to be joined up using the smallest amount of track possible, but only by joining stations directly to other stations. The distances between each of the dots represents the ‘weight ‘ of the arc. What is the best solution?

Imagine the dots on this page represent underground stations. The stations need to be joined up using the smallest amount of track possible, but only by joining stations directly to other stations. The distances between each of the dots represents the ‘weight ‘ of the arc. What is the best solution?

Kruskal’s Algorithm (To find a minimum spanning tree) Step 1:Sort the edges in ascending order of weight Step 2:Select the edge of least weight Step 3:Select, from the edges not previously selected, the edge of least weight that does not form a cycle together with edges already included Step 4:Repeat step 3 until all the vertices are connected.

We model the situation as a network, then the problem is to find the minimum connector for the network A F B C D E

A F B C D E List the edges in order of size: ED 2 AB 3 AE 4 CD 4 BC 5 EF 5 CF 6 AF 7 BF 8 CF 8 Kruskal’s Algorithm

Select the shortest edge in the network ED 2 Kruskal’s Algorithm A F B C D E

Select the next shortest edge which does not create a cycle ED 2 AB 3 Kruskal’s Algorithm A F B C D E

Select the next shortest edge which does not create a cycle ED 2 AB 3 CD 4 (or AE 4) Kruskal’s Algorithm A F B C D E

Select the next shortest edge which does not create a cycle ED 2 AB 3 CD 4 AE 4 Kruskal’s Algorithm A F B C D E

Select the next shortest edge which does not create a cycle ED 2 AB 3 CD 4 AE 4 BC 5 – forms a cycle EF 5 Kruskal’s Algorithm A F B C D E

All vertices have been connected. The solution is ED 2 AB 3 CD 4 AE 4 EF 5 Total weight of tree: 18 Kruskal’s Algorithm A F B C D E

Prim’s Algorithm (To find a minimum spanning tree) Step 1:Choose a starting vertex Step 2:Choose the vertex ‘nearest’ to the starting vertex and the associated edge to the tree Step 3:Connect to the tree the vertex that is ‘nearest’ to any vertex in the connected set Step 4:Repeat step 3 until all the vertices are connected.

A F B C D E Select any vertex A Select the shortest edge connected to that vertex AB 3 Prim’s Algorithm

A F B C D E Select the shortest edge connected to any vertex already connected. AE 4 Prim’s Algorithm

Select the shortest edge connected to any vertex already connected. ED 2 Prim’s Algorithm A F B C D E

Select the shortest edge connected to any vertex already connected. DC 4 Prim’s Algorithm A F B C D E

Select the shortest edge connected to any vertex already connected. CB 5 – forms a cycle EF 5 Prim’s Algorithm A F B C D E

A F B C D E All vertices have been connected. The solution is ED 2 AB 3 CD 4 AE 4 EF 5 Total weight of tree: 18

Apply both Prim’s and Kruskal’s algorithms to the networks below to find a minimum spanning tree. What is the weight of the minimum spanning tree in each case? 1)The algorithms will always find a minimum spanning tree, but do they always find the same minimum spanning tree? 2)Try and think of an advantage/disadvantage of each algorithm.

Apply both Prim’s and Kruskal’s algorithms to the networks below to find a minimum spanning tree. What is the weight of the minimum spanning tree in each case? 1)The algorithms will always find a minimum spanning tree, but do they always find the same minimum spanning tree? 2)Try and think of an advantage/disadvantage for each algorithm. A solution – there are others. Weight 22.A solution – there are others. Weight 28.

Both algorithms will always give solutions with the same length. They will usually select edges in a different order – you must show this in your workings. Occasionally they will use different edges – this may happen when you have to choose between edges with the same length. In this case there is more than one minimum connector for the network. Some points to note

The advantage of Prim’s algorithm is that you don’t have to check for cycles. This means that you can apply the algorithm easily without ‘seeing’ the network, just by having the matrix. This makes it ideal to program into a computer. Apply Prim’s algorithm to the network below, then write down a matrix for the network

PRIM’S ALGORITHM FOR A MATRIX Step 1: Start anywhere, say A. Cross out the row for this vertex and put an arrow over the corresponding column. Step 2: Look down all arrowed columns and find the smallest value that is not crossed out. Cross out the remainder of this row and put an arrow over the column corresponding to the row vertex. Step 3: Repeat step 2 until all columns have arrows

Write down a matrix for the network and use it to find a minimum spanning tree using the algorithm on the matrix. A   B  C D  E F  G

Network Matrix ABCDEFG A B C D E F G

Prim’s algorithm in matrix form Step 1: Start anywhere, say A.Cross out the row for this vertex and put an arrow over the corresponding column. Step 2: Look down all arrowed columns and find the smallest value that is not crossed out. Cross out the remainder of this row and put an arrow over the column corresponding to the row vertex. Step 3: Repeat step 2 until all columns have arrows

ABCDEFG A B C D E F G

Apply Prim’s algorithm to these matrices:

Lesson Objective: Finding the shortest route between two nodes in a Network A C D B F E G Look at the network below. If the weights represents the distances between the nodes, what is the shortest route from A to G?

Djikstra’s algorithm is an algorithm used to solve this kind of problem. It works by scanning the network from A to G labelling them one at a time in the search for an optimal path. It only permanently labels (or ‘boxes’) a node when it can guarantee that there is no shorter route to that node from the start position.

Dijkstra’s Algorithm finds the shortest path from the start vertex to every other vertex in the network. We will find the shortest path from A to G A C D B F E G

Dijkstra’s Algorithm 1.Label the start vertex with permanent label 0 and order label 1 2Assign temporary labels to all the vertices that can be reached directly from the start 3Select the vertex with the smallest temporary label and make its label permanent. Add the correct order label. 4Put temporary labels on each vertex that can be reached directly from the vertex you have just made permanent. The temporary label must be equal to the sum of the permanent label and the direct distance from it. If there is an existing temporary label at a vertex, it should be replaced only if the new sum is smaller. 5Select the vertex with the smallest temporary label and make its label permanent. Add the correct order label. 6Repeat until the finishing vertex has a permanent label. 7To find the shortest paths(s), trace back from the end vertex to the start vertex. Write the route forwards and state the length.

Dijkstra’s Algorithm Order in which vertices are labelled. Distance from A to vertex Working A C D B F E G Label vertex A 1 as it is the first vertex labelled

Dijkstra’s Algorithm A C D B F E G We update each vertex adjacent to A with a ‘working value’ for its distance from A. 1 0

Dijkstra’s Algorithm A C D B F E G Vertex C is closest to A so we give it a permanent label 3. C is the 2 nd vertex to be permanently labelled. 1 0

Dijkstra’s Algorithm We update each vertex adjacent to C with a ‘working value’ for its total distance from A, by adding its distance from C to C’s permanent label of A C D B F E G < 7 so replace the t-label here

Dijkstra’s Algorithm A C D B F E G The vertex with the smallest temporary label is B, so make this label permanent. B is the 3 rd vertex to be permanently labelled. 3 4

Dijkstra’s Algorithm A C D B F E G We update each vertex adjacent to B with a ‘working value’ for its total distance from A, by adding its distance from B to B’s permanent label of < 6 so replace the t-label here

Dijkstra’s Algorithm A C D B F E G The vertex with the smallest temporary label is D, so make this label permanent. D is the 4 th vertex to be permanently labelled. 4 5

Dijkstra’s Algorithm A C D B F E G We update each vertex adjacent to D with a ‘working value’ for its total distance from A, by adding its distance from D to D’s permanent label of 5. 7 < 8 so replace the t-label here < 8 so replace the t-label here 7

Dijkstra’s Algorithm A C D B F E G The vertices with the smallest temporary labels are E and F, so choose one and make the label permanent. E is chosen - the 5 th vertex to be permanently labelled. 5 7

Dijkstra’s Algorithm A C D B F E G We update each vertex adjacent to E with a ‘working value’ for its total distance from A, by adding its distance from E to E’s permanent label of 7. 9 < 12 so replace the t-label here 9

Dijkstra’s Algorithm A C D B F E G The vertex with the smallest temporary label is F, so make this label permanent.F is the 6 th vertex to be permanently labelled

Dijkstra’s Algorithm A C D B F E G We update each vertex adjacent to F with a ‘working value’ for its total distance from A, by adding its distance from F to F’s permanent label of > 9 so do not replace the t-label here

Dijkstra’s Algorithm A C D B F E G G is the final vertex to be permanently labelled. 7 9

Dijkstra’s Algorithm A C D B F E G To find the shortest path from A to G, start from G and work backwards, choosing arcs for which the difference between the permanent labels is equal to the arc length. The shortest path is ABDEG, with length 9.

First of all draw boxes as shown next to each of the nodes.

Text book page 12,13 and 11 in that order page Ex 11E