Download presentation
Presentation is loading. Please wait.
1
Graphs Discrete Structure CS203
2
Matrices
3
Adjacency Matrix -Directed Multigraphs
Can easily generalize to directed multigraphs by putting in the number of edges between vertices, instead of only allowing 0 and 1: For a directed multigraph G = (V,E ) define the matrix AG by: Rows, Columns –one for each vertex in V Value at i th row and j th column is The number of edges with source the i th vertex and target the j th vertex
4
Adjacency Matrix Representation for a Directed Graph
1 2 3 4 5 1 2 3 5 4
5
Adjacency Matrix -Directed Multigraphs
2 1 4 3
6
Adjacency Matrix -General
1 2 A: Notice that answer is symmetric. 3 4
7
Counting Paths Let A be the Adjacency Matrix. What is A2? b a c d e
8
Paths Q: Find a longest possible simple path in the following graph:
1 2 e5 e3 e4 e7 3 4
9
Paths in Directed Graphs
Q: Consider digraph adjacency matrix: Find a path from 1 to 4. Is there a path from 4 to 1?
10
Euler Paths and Circuits
An Euler path in a graph G is a simple path containing every edge in G. An Euler circuit (or Euler cycle) is a cycle which is an Euler path. NOTE: The definition applies both to undirected as well as directed graphs of all types. NOTE: the graph is Euler circuit then every node is even degree
11
Hamilton Paths and Circuits
A Hamilton path in a graph G is a path which visits ever vertex in G exactly once. A Hamilton circuit (or Hamilton cycle) is a cycle which visits every vertex exactly once, except for the first vertex, which is also visited at the end of the cycle. NOTE: Again, the definition applies both to undirected as well as directed graphs of all types.
12
Hamilton Paths and Cycles
a path or cycle that contain every vertex Unlike Euler circuit, there is no known necessary and sufficient condition for a graph to be Hamiltonian. c a b e d f There is a Hamilton path, but no Hamilton cycle. h g i
13
Shortest Path Problems
We can assign weights to the edges of graphs, for example to represent the distance between cities in a railway network: Chicago Toronto New York Boston 600 700 200 650
14
Shortest Path Problems
Such weighted graphs can also be used to model computer networks with response times or costs as weights. What is the shortest path between two vertices in the graph, that is, the path with the minimal sum of weights along the way? This corresponds to the shortest train connection or the fastest connection in a computer network.
15
Dijkstra’s Algorithm procedure Dijkstra(G: weighted connected simple graph with vertices a = v0, v1, …, vn = z and positive weights w(vi, vj), where w(vi, vj) = if {vi, vj} is not an edge in G) for i := 1 to n L(vi) := L(a) := 0 S := {the labels are now initialized so that the label of a is zero and all other labels are , and the distinguished set of vertices S is empty}
16
Dijkstra’s Algorithm while zS begin
u := the vertex not in S with minimal L(u) S := S{u} for all vertices v not in S if L(u) + w(u, v) < L(v) then L(v) := L(u) + w(u, v) {this adds a vertex to S with minimal label and updates the labels of vertices not in S} end {L(z) = length of shortest path from a to z}
17
Dijkstra’s Algorithm Example: a b d z e c 5 6 4 8 1 2 3 2 10
3 2 10 Step 0
18
Dijkstra’s Algorithm Example: a b d z e c 4 2 1 5 8 10 6 3 4 (a)
4 (a) 2 (a) Step 1
19
Dijkstra’s Algorithm Example: a b d z e c 4 2 1 5 8 10 6 3 3 (a, c)
3 (a, c) 4 (a) 10 (a, c) 2 (a) 12 (a, c) Step 2
20
Dijkstra’s Algorithm Example: a b d z e c 4 2 1 5 8 10 6 3 3 (a, c)
3 (a, c) 4 (a) 8 (a, c, b) 10 (a, c) 2 (a) 12 (a, c) Step 3
21
Dijkstra’s Algorithm Example: a b d z e c 4 2 1 5 8 10 6 3 3 (a, c)
3 (a, c) 4 (a) 8 (a, c, b) 10 (a, c) 14 (a, c, b, d) 2 (a) 12 (a, c) 10 (a, c, b, d) Step 4
22
Dijkstra’s Algorithm Example: a b d z e c 4 2 1 5 8 10 6 3 4 (a)
4 (a) 3 (a, c) 8 (a, c, b) 10 (a, c) 14 (a, c, b, d) 13 (a, c, b, d, e) 2 (a) 12 (a, c) 10 (a, c, b, d) Step 5
23
Dijkstra’s Algorithm Example: a b d z e c 4 2 1 5 8 10 6 3 4 (a)
4 (a) 3 (a, c) 8 (a, c, b) 10 (a, c) 14 (a, c, b, d) 13 (a, c, b, d, e) 2 (a) 12 (a, c) 10 (a, c, b, d) Step 6
24
The Traveling Salesman Problem
The traveling salesman problem is one of the classical problems in computer science. A traveling salesman wants to visit a number of cities and then return to his starting point. Of course he wants to save time and energy, so he wants to determine the shortest path for his trip. The problem then is to find the circuit of minimum total weight that visits each vertex exactly once.
25
The Traveling Salesman Problem
Example: What path would the traveling salesman take to visit the following cities? Chicago Toronto New York Boston 600 700 200 650 550 Solution: The shortest path is Boston, New York, Chicago, Toronto, Boston (2,000 miles).
26
Trees Definition: A tree is a connected undirected graph with no simple circuits. Since a tree cannot have a simple circuit, a tree cannot contain multiple edges or loops. Therefore, any tree must be a simple graph. Theorem: An undirected graph is a tree if and only if there is a unique simple path between any of its vertices.
27
Trees Example: Are the following graphs trees? Yes. No. No. Yes.
28
Tree Terminology If v is a vertex in a rooted tree other than the root, the parent of v is the unique vertex u such that there is a directed edge from u to v. When u is the parent of v, v is called the child of u. Vertices with the same parent are called siblings. The ancestors of a vertex other than the root are the vertices in the path from the root to this vertex, excluding the vertex itself and including the root.
29
Tree Terminology The descendants of a vertex v are those vertices that have v as an ancestor. A vertex of a tree is called a leaf if it has no children. Vertices that have children are called internal vertices. If a is a vertex in a tree, then the subtree with a as its root is the subgraph of the tree consisting of a and its descendants and all edges incident to these descendants.
30
Trees Example I: Family tree James Christine Bob Frank Joyce Petra
31
Trees Example III: Arithmetic expressions + - y z x y
This tree represents the expression (y + z)(x - y).
32
Binary Search Trees If we want to perform a large number of searches in a particular list of items, it can be worthwhile to arrange these items in a binary search tree to facilitate the subsequent searches. A binary search tree is a binary tree in which each child of a vertex is designated as a right or left child, and each vertex is labeled with a key, which is one of the items. When we construct the tree, vertices are assigned keys so that the key of a vertex is both larger than the keys of all vertices in its left subtree and smaller than the keys of all vertices in its right subtree.
33
Spanning Trees Definition: Let G be a simple graph. A spanning tree of G is a subgraph of G that is a tree containing every vertex of G. Note: A spanning tree of G = (V, E) is a connected graph on V with a minimum number of edges (|V| - 1). Example: Since winters in Boston can be very cold, six universities in the Boston area decide to build a tunnel system that connects their libraries.
34
Spanning Trees The complete graph including all possible tunnels:
Brandeis Harvard MIT BU Tufts UMass The spanning trees of this graph connect all libraries with a minimum number of tunnels.
35
Spanning Trees Example for a spanning tree: Brandeis Harvard MIT Tufts
BU UMass Since there are 6 libraries, 5 tunnels are sufficient to connect all of them.
36
Coloring graph To be reported
37
Application to Ad Hoc Networking
Networks can be represented by graphs The mobile nodes are vertices The communication links are edges Vertices Edges Routing protocols often use shortest path algorithms This lecture is background material to the routing algorithms
38
Graphs ↔ Networks Graph (Network) Vertexes (Nodes) Edges (Arcs) Flow
Communications Telephones exchanges, computers, satellites Cables, fiber optics, microwave relays Voice, video, packets Circuits Gates, registers, processors Wires Current Mechanical Joints Rods, beams, springs Heat, energy Hydraulic Reservoirs, pumping stations, lakes Pipelines Fluid, oil Financial Stocks, currency Transactions Money Transportation Airports, rail yards, street intersections Highways, railbeds, airway routes Freight, vehicles, passengers
39
Network data sets Collaboration graphs Who-talks-to-whom graphs
Wikipedia, World of Warcraft, Citation graphs Who-talks-to-whom graphs Microsoft IM, Cell phone graphs Information linkage Hyperlinks Technological networks Power grids, communication links, Internet Natural and biological networks Food webs, neural interconnections, cell metabolism
40
Recap A graph consists of nodes and edges
Graphs can be directed or undirected, weighted, signed or unweighted Paths between nodes (simple vs. non-simple) Cycles Connectivity Components (and the giant component) Distance (and BFS) Six degrees of separation can be checked with BFS
41
Distance Def: The distance between a pair of nodes is the edge length of the shortest path between them Just number of edges. Can be thought of as all edges having weight of 1 What‘s the distance between MIT and SDC?
42
Distance: Breadth-first search (BFS)
43
Link Failure Simple rerouting case F detects that link to G has failed
B Simple rerouting case E C A F detects that link to G has failed F sets a distance of ∞ to G and sends update to A A sets a distance of ∞ to G since it uses F to reach G A receives periodic update from C with 2-hop path to G (via D) A sets distance to G to 3 and sends update to F F decides it can reach G in 4 hops via A D F G
44
Link Failure This behavior is called count-to-infinity
Routing loop case E Link from A to E fails A advertises distance of ∞ to E B and C had advertised a distance of 2 to E (prior to the link failure) Upon reception of A’s routing update B decides it can reach E in 3 hops; and advertises this to A A decides it can read E in 4 hops; advertises this to C C decides that it can reach E in 5 hops… C A D F G This behavior is called count-to-infinity
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.