2012: J Paul GibsonT&MSP: Mathematical FoundationsMAT7003/L2-GraphsAndTrees.1 MAT 7003 : Mathematical Foundations (for Software Engineering) J Paul Gibson,

Slides:



Advertisements
Similar presentations
CSE 211 Discrete Mathematics
Advertisements

Trees Chapter 11.
Cpt S 223 – Advanced Data Structures Graph Algorithms: Introduction
Chapter 9 Graphs.
Lecture 5 Graph Theory. Graphs Graphs are the most useful model with computer science such as logical design, formal languages, communication network,
22C:19 Discrete Structures Trees Spring 2014 Sukumar Ghosh.
Graphs Chapter 12. Chapter Objectives  To become familiar with graph terminology and the different types of graphs  To study a Graph ADT and different.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 21: Graphs.
Applied Discrete Mathematics Week 12: Trees
Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.
Introduction to Graphs
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
Spring 2010CS 2251 Graphs Chapter 10. Spring 2010CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs.
Fall 2007CS 2251 Graphs Chapter 12. Fall 2007CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs To.
Important Problem Types and Fundamental Data Structures
Discrete Mathematics Lecture 9 Alexander Bukharovich New York University.
C o n f i d e n t i a l HOME NEXT Subject Name: Data Structure Using C Unit Title: Trees.
KNURE, Software department, Ph , N.V. Bilous Faculty of computer sciences Software department, KNURE The trees.
GRAPH Learning Outcomes Students should be able to:
Data Structures Using C++ 2E
May 5, 2015Applied Discrete Mathematics Week 13: Boolean Algebra 1 Dijkstra’s Algorithm procedure Dijkstra(G: weighted connected simple graph with vertices.
Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 6-1 Chapter 6 Graphs Introduction to Data Structure CHAPTER 6 GRAPHS 6.1 The Graph Abstract Data Type.
Graph Theoretic Concepts. What is a graph? A set of vertices (or nodes) linked by edges Mathematically, we often write G = (V,E)  V: set of vertices,
Chapter 2 Graph Algorithms.
GRAPHS CSE, POSTECH. Chapter 16 covers the following topics Graph terminology: vertex, edge, adjacent, incident, degree, cycle, path, connected component,
May 1, 2002Applied Discrete Mathematics Week 13: Graphs and Trees 1News CSEMS Scholarships for CS and Math students (US citizens only) $3,125 per year.
Foundations of Discrete Mathematics
© by Kenneth H. Rosen, Discrete Mathematics & its Applications, Sixth Edition, Mc Graw-Hill, 2007 Chapter 9 (Part 2): Graphs  Graph Terminology (9.2)
Week 11 - Wednesday.  What did we talk about last time?  Graphs  Euler paths and tours.
Tree A connected graph that contains no simple circuits is called a tree. Because a tree cannot have a simple circuit, a tree cannot contain multiple.
1 CS104 : Discrete Structures Chapter V Graph Theory.
GRAPHS THEROY. 2 –Graphs Graph basics and definitions Vertices/nodes, edges, adjacency, incidence Degree, in-degree, out-degree Subgraphs, unions, isomorphism.
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.
Data Structures & Algorithms Graphs
Agenda Review: –Planar Graphs Lecture Content:  Concepts of Trees  Spanning Trees  Binary Trees Exercise.
COSC 2007 Data Structures II Chapter 14 Graphs I.
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.
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.
Discrete Structures CISC 2315 FALL 2010 Graphs & Trees.
Chapter 10: Trees A tree is a connected simple undirected graph with no simple circuits. Properties: There is a unique simple path between any 2 of its.
Chapter 9: Graphs.
Chapter 20: Graphs. Objectives In this chapter, you will: – Learn about graphs – Become familiar with the basic terminology of graph theory – Discover.
Graphs Definition: a graph is an abstract representation of a set of objects where some pairs of the objects are connected by links. The interconnected.
Chapter 05 Introduction to Graph And Search Algorithms.
Data Structures and Algorithm Analysis Graph Algorithms Lecturer: Jing Liu Homepage:
1 GRAPH Learning Outcomes Students should be able to: Explain basic terminology of a graph Identify Euler and Hamiltonian cycle Represent graphs using.
Chapter 11. Chapter Summary  Introduction to trees (11.1)  Application of trees (11.2)  Tree traversal (11.3)  Spanning trees (11.4)
Lecture 20. Graphs and network models 1. Recap Binary search tree is a special binary tree which is designed to make the search of elements or keys in.
Discrete Structures Li Tak Sing( 李德成 ) Lectures
Code: BCA302 Data Structures with C Prof. (Dr.) Monalisa Banerjee By.
Trees.
Applied Discrete Mathematics Week 15: Trees
Graph Graphs and graph theory can be used to model:
Chapter 5 : Trees.
Graph theory Definitions Trees, cycles, directed graphs.
12. Graphs and Trees 2 Summary
Lecture 18. Basics and types of Trees
Advanced Algorithms Analysis and Design
Taibah University College of Computer Science & Engineering Course Title: Discrete Mathematics Code: CS 103 Chapter 10 Trees Slides are adopted from “Discrete.
Graphs Chapter 13.
Graphs Chapter 11 Objectives Upon completion you will be able to:
What is a Graph? a b c d e V= {a,b,c,d,e} E= {(a,b),(a,c),(a,d),
Chapter 11 Graphs.
Trees-2, Graphs Data Structures with C Chpater-6 Course code: 10CS35
GRAPHS Lecture 17 CS2110 Spring 2018.
Important Problem Types and Fundamental Data Structures
Chapter 14 Graphs © 2011 Pearson Addison-Wesley. All rights reserved.
Applied Discrete Mathematics Week 13: Graphs
Presentation transcript:

2012: J Paul GibsonT&MSP: Mathematical FoundationsMAT7003/L2-GraphsAndTrees.1 MAT 7003 : Mathematical Foundations (for Software Engineering) J Paul Gibson, A207 Graphs and Trees

2012: J Paul GibsonT&MSP: Mathematical FoundationsMAT7003/L2-GraphsAndTrees.2 Graphs A graph is a: set of points, and lines connecting some (possibly empty) subset of the points. The points of a graph are most commonly known as graph vertices, but may also be called "nodes" or simply "points." (NOTE: every vertex in a graph must be distinguishable from every other vertex in that graph.) Similarly, the lines connecting the vertices of a graph are most commonly known as graph edges, but may also be called "arcs" or "lines."

2012: J Paul GibsonT&MSP: Mathematical FoundationsMAT7003/L2-GraphsAndTrees.3 Graphs come in a wide variety of different sorts The most common type is graphs in which at most one edge (i.e., either one edge or no edges) may connect any two vertices. Such graphs are called simple graphs.

2012: J Paul GibsonT&MSP: Mathematical FoundationsMAT7003/L2-GraphsAndTrees.4 Graphs come in a wide variety of different sorts If multiple edges are allowed between vertices, the graph is known as a multigraph.

2012: J Paul GibsonT&MSP: Mathematical FoundationsMAT7003/L2-GraphsAndTrees.5 Graphs come in a wide variety of different sorts Vertices are usually not allowed to be self- connected, but this restriction is sometimes relaxed to allow such "graph loops." A graph that may contain multiple edges and graph loops is called a pseudograph.

2012: J Paul GibsonT&MSP: Mathematical FoundationsMAT7003/L2-GraphsAndTrees.6 Graphs come in a wide variety of different sorts: labellings The edges, vertices, or both of a graph may be assigned specific values, labels, or colors, in which case the graph is called a labeled graph.

2012: J Paul GibsonT&MSP: Mathematical FoundationsMAT7003/L2-GraphsAndTrees.7 Graphs come in a wide variety of different sorts: colourings A vertex coloring is an assignment of labels or colors to each vertex of a graph such that no edge connects two identically colored vertices. An edge coloring is an assignment of labels or colors to each edge of a graph such that adjacent edges (or the edges bounding different regions) must receive different colors. The assignment of labels or colors to the edges or vertices of a graph based on a set of specified criteria is known as graph coloring.

2012: J Paul GibsonT&MSP: Mathematical FoundationsMAT7003/L2-GraphsAndTrees.8 Graphs come in a wide variety of different sorts - directedness If arrows may be placed on one or both endpoints of the edges of a graph to indicate directedness, the graph is said to be directed ( or a digraph); otherwise it is undirected. A directed graph in which each edge is given a unique is called an oriented graph. A graph or directed graph together with a function which assigns a positive real number to each edge (i.e., an oriented edge-labeled graph) is known as a network.

2012: J Paul GibsonT&MSP: Mathematical FoundationsMAT7003/L2-GraphsAndTrees.9 Graphs as adjacency matrices We can use a matrix to represent a graph’s structure.

2012: J Paul GibsonT&MSP: Mathematical FoundationsMAT7003/L2-GraphsAndTrees.10 Graphs as adjacency lists We can also use lists to represent a graph’s structure.

2012: J Paul GibsonT&MSP: Mathematical FoundationsMAT7003/L2-GraphsAndTrees.11 The degree (or valence) of a vertex is the number of edge ends at that vertex. The out-degree of a node in a digraph is: the number of edges emanating from that node The in-degree of a node in a digraph is: the number of edges incident on that node A source has in-degree=0, A sink has outdegree=0 Graphs: more terminology and definitions QUESTION: What is out-degree of source? What is in-degree of sink?

2012: J Paul GibsonT&MSP: Mathematical FoundationsMAT7003/L2-GraphsAndTrees.12 A path is a sequence of consecutive edges in a graph and the length of the path is the number of edges traversed. Graphs: more terminology and definitions

2012: J Paul GibsonT&MSP: Mathematical FoundationsMAT7003/L2-GraphsAndTrees.13 The diameter of a connected graph (component) is the length of the longest shortest path between 2 nodes of the graph (component) Graphs: more terminology and definitions

2012: J Paul GibsonT&MSP: Mathematical FoundationsMAT7003/L2-GraphsAndTrees.14 Graphs: more terminology and definitions QUESTION: find a path, cycle, loop and simple cycle in graph G1

2012: J Paul GibsonT&MSP: Mathematical FoundationsMAT7003/L2-GraphsAndTrees.15 Graphs: more terminology and definitions Two vertices are adjacent if they are connected by an edge. A complete graph with n vertices (denoted Kn) is a graph with n vertices in which each vertex is connected to each of the others (with one edge between each pair of vertices A graph is connected if there is a path connecting every pair of vertices. A graph that is not connected can be divided into connected components (disjoint connected subgraphs). A vertex of degree zero (with no edges connected) is isolated A vertex of degree one (with only one edge connected) is a pendant edge.

2012: J Paul GibsonT&MSP: Mathematical FoundationsMAT7003/L2-GraphsAndTrees.16 Directed Acyclic Graphs (DAGs) For certain applications it is convenient to deal with graphs that contain no cycles. For example, a tree is a special kind of graph that contains no cycles. DEFINITION: A directed, acyclic graph is a directed graph that contains no cycles. NOTE: G2 is a particular type of DAG – a tree

2012: J Paul GibsonT&MSP: Mathematical FoundationsMAT7003/L2-GraphsAndTrees.17 Graph Traversals There are many different applications of graphs. As a result, there are many different algorithms for manipulating them. Many of the different graph algorithms systematically visit all the vertices in the graph. That is, the algorithm walks through the graph data structure and performs some computation at each vertex in the graph. This process of walking through the graph is called a graph traversal. While there are many different possible ways in which to systematically visit all the vertices of a graph, the most common are: depth-first traversal, breadth-first traversal and topological sort.

2012: J Paul GibsonT&MSP: Mathematical FoundationsMAT7003/L2-GraphsAndTrees.18 Graph Traversals: depth-first Depth-first traversal of a graph visits a start-vertex and then recursively visits all the vertices adjacent to that node. The graph may contain cycles, but the traversal must visit every vertex at most once. The solution to the problem is to keep track of the nodes that have been visited, so that the traversal does not suffer the fate of infinite recursion. For example:

2012: J Paul GibsonT&MSP: Mathematical FoundationsMAT7003/L2-GraphsAndTrees.19 Graph Traversals: breadth first Since a graph has no root, when we do a breadth-first traversal, we must specify the vertex at which to start the traversal. Furthermore, we can define the depth of a given vertex to be the length of the shortest path from the starting vertex to the given vertex. Thus, breadth-first traversal first visits the starting vertex, then all the vertices adjacent to the starting vertex, and the all the vertices adjacent to those, and so on.

2012: J Paul GibsonT&MSP: Mathematical FoundationsMAT7003/L2-GraphsAndTrees.20 Graph Traversals: topological sort A topological sort or topological ordering of a directed acyclic graph (DAG) is a linear ordering of its nodes in which each node comes before all nodes to which it has outbound edges. Every DAG has one or more topological sorts.

2012: J Paul GibsonT&MSP: Mathematical FoundationsMAT7003/L2-GraphsAndTrees.21 Graph Algorithms There are many interesting graph problems and algorithmic solutions, e.g: Traversal Searching Shortest/Longest Paths Eulerian Hamiltonian Circuits Max/Min flow Flooding Isomorphisms Reduction Re-writing Color coding Spanning Trees TO DO: You need to have a broad understanding of all of these problems They arise in many different problem domains and you should be aware of classical solutions that can be re-use/adapted We return to some of these in later lectures when we look at computational complexity

2012: J Paul GibsonT&MSP: Mathematical FoundationsMAT7003/L2-GraphsAndTrees.22 Graph Algorithms: transitive closure The diameter of the transitive closure is 1

2012: J Paul GibsonT&MSP: Mathematical FoundationsMAT7003/L2-GraphsAndTrees.23 QUESTION: What is the transitive closure of the following graph

2012: J Paul GibsonT&MSP: Mathematical FoundationsMAT7003/L2-GraphsAndTrees.24 Answer: Two connected subpgraphs

2012: J Paul GibsonT&MSP: Mathematical FoundationsMAT7003/L2-GraphsAndTrees.25 Trees A tree is a graph in which any two vertices are connected by exactly one simple path. In other words, any connected graph without cycles is a tree. A forest is a disjoint union of trees. A tree is called a rooted tree if one vertex has been designated the root, in which case the edges have a natural orientation, towards or away from the root. The tree-order is the partial ordering on the vertices of a tree with u ≤ v if and only if the unique path from the root to v passes through u. In a rooted tree, the parent of a vertex is the vertex connected to it on the path to the root; every vertex except the root has a unique parent. A child of a vertex v is a vertex of which v is the parent. A leaf is a vertex without children.

2012: J Paul GibsonT&MSP: Mathematical FoundationsMAT7003/L2-GraphsAndTrees.26 Trees: some specialisations An n-ary tree is a rooted tree for which each vertex which is not a leaf has at most n children. 2-ary trees (resp. 3-ary trees) are sometimes called binary trees (resp. ternary trees) A binary search tree (BST) is a node based binary tree data structure (where each node has a data value or key that are ordered) which has the following properties: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of a node contains only nodes with keys greater than the node's key. Both the left and right subtrees must also be binary search trees.

2012: J Paul GibsonT&MSP: Mathematical FoundationsMAT7003/L2-GraphsAndTrees.27 Trees: balanced binary search tree Definition: A balanced binary search tree requires that: The tree is a binary search tree, and it is balanced: The height of the two subtrees (children) of a node differs by at most one. (Where, the height is the maximum distance of any leaf from the root of a tree) Each subtree is balanced For example:

2012: J Paul GibsonT&MSP: Mathematical FoundationsMAT7003/L2-GraphsAndTrees.28 Trees: AVL tree The AVL tree is named after its two inventors, G.M. Adelson- Velskii and E.M. Landis, who published it in their 1962 paper “An algorithm for the organization of information.” An AVL tree is a self-balancing binary search tree, and it is the first such data structure to be invented TO DO: Check that you know how insertions and deletions work for such AVL trees

2012: J Paul GibsonT&MSP: Mathematical FoundationsMAT7003/L2-GraphsAndTrees.29 Trees: some specialisations A heap is a specialized binary tree where if B is a child node of A, then key(A) ≥ key(B). For example:

2012: J Paul GibsonT&MSP: Mathematical FoundationsMAT7003/L2-GraphsAndTrees.30 Trees from graphs – minimum spanning tree Informally, a spanning tree of a graph G is a selection of edges of G that form a tree spanning every vertex. That is, every vertex lies in the tree, but no cycles (or loops) are formed. EXAMPLE: It is often useful to find a minimum spanning tree of a weighted graph, ie a spanning tree with weight less than or equal to the weight of every other spanning tree. NOTE: The weight is defined as the sum of the weights of the edges in that spanning tree.

2012: J Paul GibsonT&MSP: Mathematical FoundationsMAT7003/L2-GraphsAndTrees.31 Representing Graphs As Text Strings There are lots of ways of representing graphs textually. For example, consider graph G and an alphabet restricted to: Integers as node labels Letters as arc labels Standard punctuation – for representing structure I could choose a string representation such as:

2012: J Paul GibsonT&MSP: Mathematical FoundationsMAT7003/L2-GraphsAndTrees.32 Representing Graphs As Text Strings QUESTION: What other string representations can you find (using the same alphabet of punctuation): i.[ ii.] iii., iv.( v.) QUESTION: Can we reduce the amont of punctuation required?

2012: J Paul GibsonT&MSP: Mathematical FoundationsMAT7003/L2-GraphsAndTrees.33 Representing Graphs As Text Strings TYPICAL ANSWERS: QUESTION: Compare and contrast the different representations … are they equivalent?

2012: J Paul GibsonT&MSP: Mathematical FoundationsMAT7003/L2-GraphsAndTrees.34 Programming Exercise: check for cycles? For any graph, such as G, node and arc labelled, with directed arcs, chose a textual (string) representation. Write a function/method/procedure (in the programming language of your choice) that reads the string representation and returns true if the given graph contains a cycle (loop) NOTE: Checking for cycles is very important because many graph algorithms work only if there are no cycles in the graph (otherwise they may get into an infinite loop)