Skip to content
Snippets Groups Projects
Commit 04e0acdb authored by Peter Gerwinski's avatar Peter Gerwinski
Browse files

Vortragsfolien, Tafelbild und Beispielprogramme 21.6.2018

parent 51ff29be
No related branches found
No related tags found
No related merge requests found
20180614/photo-20180614-144953.jpg

70.9 KiB

20180614/photo-20180614-145029.jpg

60.5 KiB

20180614/photo-20180614-145058.jpg

79.5 KiB

#include <stdio.h>
#include <limits.h>
#include <vector>
using namespace std;
/*
* Function to print the whole graph.
*/
void printGraph (vector <vector <int> > graph)
{
for (unsigned int i = 0; i < graph.size (); i++)
{
for (unsigned int j = 0; j < graph.size (); j++)
{
printf ("%d\t", graph[j][i]);
}
printf ("\n");
}
printf ("\n");
}
/*
* Function to print shortest path from source node to j.
*/
void printPath (int shortestPath[], int j)
{
if (shortestPath[j] != -1)
{
printPath (shortestPath, shortestPath[j]);
printf ("%d ", j);
}
}
/*
* Function to print the distances and shortest paths of transitions from
* source node to every other using the dijkstra algorithm results.
*/
void printResult (int src, vector <int> distances, int shortestPath[])
{
printf ("Transition\t Distance\t Path");
for (unsigned int i = 1; i < distances.size (); i++)
{
if (distances[i] == INT_MAX)
{
printf ("\n%d -> %d \t\t \t\t NONE ", src, i);
}
else
{
printf ("\n%d -> %d \t\t %d\t\t %d ", src, i, distances[i], src);
printPath (shortestPath, i);
}
}
printf ("\n");
}
/*
* Function to process the Dijkstra algorithm.
*/
void dijkstra (vector <vector <int> > graph, int src)
{
int N = graph.size ();
vector <int> distances (N);
vector <bool> visited (N);
int shortestPath[N];
// Initially all distances are infinity and not visited
for (int i = 0; i < N; i++)
{
shortestPath[0] = -1;
distances[i] = INT_MAX;
visited[i] = false;
}
// Distance to source node itself is 0
distances[src] = 0;
// Find shortest paths
for (int n = 0; n < N; n++)
{
// Pick the minimum distance node from non visited nodes.
int min = INT_MAX;
int minIdx = 0;
for (int i = 0; i < N; i++)
{
if (visited[i] == false && distances[i] <= min)
{
min = distances[i];
minIdx = i;
}
}
// Set node as visited
visited[minIdx] = true;
// Update distance value of adjacent nodes
for (int j = 0; j < N; j++)
{
/*
* Update condition:
* - Node must be non visited
* - A connection (edge) from node at minIdx to node at j has to be available
* - Total path weight is smaller than current distance value
*/
if (!visited[j] && graph[minIdx][j] && distances[minIdx] + graph[minIdx][j] < distances[j])
{
shortestPath[j] = minIdx;
distances[j] = distances[minIdx] + graph[minIdx][j];
}
}
}
printResult (src, distances, shortestPath);
}
/*
* Main program.
*/
int main()
{
vector <vector <int> > graph;
// Create empty graph
int nodeCount = 10;
for (int i = 0; i < nodeCount; i++)
{
graph.push_back (vector <int> (nodeCount));
for (int j = 0; j < nodeCount; j++)
{
graph[i][j] = 0;
}
}
/*
Frankfurt = 0
Mannheim = 1
Würzburg = 2
Stuttgart = 3
Kassel = 4
Nürnberg = 5
Erfurt = 6
Karlsruhe = 7
Augsburg = 8
München = 9
*/
// Set known weights
graph[0][1] = 85; // Frankfurt-Mannheim
graph[0][2] = 217; // Frankfurt-Würzburg
graph[0][4] = 173; // Frankfurt-Kassel
graph[1][7] = 80; // Mannheim-Karlsruhe
graph[7][8] = 250; // Karlsruhe-Augsburg
graph[8][9] = 84; // Augsburg-München
graph[2][6] = 186; // Würzburg-Erfurt
graph[2][5] = 103; // Würzburg-Nürnberg
graph[3][5] = 183; // Stuttgart-Nürnberg
graph[5][9] = 167; // Nürnberg-München
graph[4][9] = 502; // Kassel-München
printf ("Initial Graph\n");
printGraph (graph);
// Run dijkstra algorithm
dijkstra (graph, 0);
return 0;
}
../common/Zeichen_123.pdf
\ No newline at end of file
File added
% ad-20180621.pdf - Lecture Slides on Algorithms and Data Structures in C/C++
% Copyright (C) 2018 Peter Gerwinski
%
% This document is free software: you can redistribute it and/or
% modify it either under the terms of the Creative Commons
% Attribution-ShareAlike 3.0 License, or under the terms of the
% GNU General Public License as published by the Free Software
% Foundation, either version 3 of the License, or (at your option)
% any later version.
%
% This document is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with this document. If not, see <http://www.gnu.org/licenses/>.
%
% You should have received a copy of the Creative Commons
% Attribution-ShareAlike 3.0 Unported License along with this
% document. If not, see <http://creativecommons.org/licenses/>.
% README: Datenorganisation: balancierte Bäume
\documentclass[10pt,t]{beamer}
\usepackage{pgslides}
\usepackage{rotating}
\usepackage{pdftricks}
\usepackage{amsfonts}
\begin{psinputs}
\usepackage[latin1]{inputenc}
\usepackage[german]{babel}
\usepackage[T1]{fontenc}
\usepackage{helvet}
\renewcommand*\familydefault{\sfdefault}
\usepackage{graphicx}
\usepackage{pstricks,pst-circ,pst-plot}
\psset{unit=0.6cm}
\psset{linewidth=0.03}
\end{psinputs}
\newcommand{\underconstruction}{%
\begin{picture}(0,0)(0,3)
\color{black}
\put(7,-2.2){\makebox(0,0)[b]{\includegraphics[width=1.5cm]{Zeichen_123.pdf}}}
\put(7,-2.5){\makebox(0,0)[t]{\shortstack{Änderungen\\vorbehalten}}}
\end{picture}}
\lstdefinestyle{math}{basicstyle=\footnotesize\color{structure},
language={},
columns=fixed,
moredelim=**[is][\color{darkred}]{¡}{¿},
moredelim=**[is][\color{darkgreen}]{°}{¿}}
\title{Algorithmen und Datenstrukturen in C/C++}
\author{Prof.\ Dr.\ rer.\ nat.\ Peter Gerwinski}
\date{21.\ Juni 2018}
\begin{document}
\maketitleframe
\nosectionnonumber{\inserttitle}
\begin{frame}
\shownosectionnonumber
\begin{itemize}
\item[\textbf{1}] \textbf{Einführung}
\underconstruction
\hfill\makebox(0,0)[br]{\raisebox{2.25ex}{\url{https://gitlab.cvh-server.de/pgerwinski/ad.git}}}
\item[\textbf{2}] \textbf{Einführung in C++}
\item[\textbf{3}] \textbf{Datenorganisation}
\item[\textbf{4}] \textbf{Datenkodierung}
\item[\textbf{5}] \textbf{Hardwarenahe Algorithmen}
\item[\textbf{6}] \textbf{Optimierung}
\begin{itemize}
\item[6.1] Einführung
\item[6.2] Beispiel: Treppenbau
\item[6.3] Beispiel: Rasenmähroboter
\color{orange}
\item[6.3] Beispiel: Routenplanung
\end{itemize}
\color{gray}
\item[\textbf{7}] \textbf{Numerik}
\end{itemize}
\end{frame}
\setcounter{section}{5}
\section{Optimierung}
\setcounter{subsection}{2}
\subsection{Beispiel: Routenplanung}
\begin{frame}
\showsection
\showsubsection
\begin{itemize}
\item
Karte bekannt
\item
Hindernissen ausweichen
\item
gewichteter Graph: Dijkstra, A*
\end{itemize}
\end{frame}
\subsection{Lineare Optimierung}
\begin{frame}
\showsection
\showsubsection
\begin{itemize}
\item
auch: \newterm{Lineare Programmierung}
\item
Anwendungen: Wirtschaftsmathematik
\item
mehrere Bedingungen der Form $a_1 x_1 + \dots + a_n x_n \le b_n$
\item
Optimierung einer Kostenfunktion $c_1 x_1 + \dots + c_n x_n$
\item
geometrische Interpretation:\\
Der zulässige Bereich ist ein $n$-dimensionales Polyeder.
\item
Simplex-Verfahren:\\
von Ecke zu Ecke hangeln, bis keine Verbesserung mehr möglich
\item
Komplexität: $\mathcal{O}(2^n)$; in der Praxis meist schneller
\end{itemize}
\end{frame}
\subsection{Problem des Handlungsreisenden}
\begin{frame}
\showsection
\showsubsection
\begin{itemize}
\item
\newterm{NP-vollständiges\/} Problem:
aufwendiger als $\mathcal{O}(n^k)$, egal für welches $k$
\item
Anwendungen: Tourenplanung, Layout integrierter Schaltkreise, Gentechnik
\end{itemize}
\end{frame}
\nosectionnonumber{\inserttitle}
\begin{frame}
\shownosectionnonumber
\begin{itemize}
\item[\textbf{1}] \textbf{Einführung}
\underconstruction
\hfill\makebox(0,0)[br]{\raisebox{2.25ex}{\url{https://gitlab.cvh-server.de/pgerwinski/ad.git}}}
\item[\textbf{2}] \textbf{Einführung in C++}
\item[\textbf{3}] \textbf{Datenorganisation}
\item[\textbf{4}] \textbf{Datenkodierung}
\item[\textbf{5}] \textbf{Hardwarenahe Algorithmen}
\begin{itemize}
\item[5.1] Zeichnen von Linien
\item[5.2] CORDIC
\item[5.3] FFT
\end{itemize}
\item[\textbf{6}] \textbf{Optimierung}
\begin{itemize}
\item[6.1] Einführung
\item[6.2] Beispiel: Treppenbau
\item[6.3] Beispiel: Rasenmähroboter
\color{medgreen}
\item[6.4] Beispiel: Routenplanung
\item[6.5] Lineare Optimierung
\item[6.6] Problem des Handlungsreisenden
\end{itemize}
\color{gray}
\item[\textbf{7}] \textbf{Numerik}
\end{itemize}
\end{frame}
\end{document}
# -*- coding: utf-8 -*-
from collections import defaultdict
class Graph:
def __init__(self):
self.nodes = set()
self.edges = defaultdict(list)
self.distances = {}
def add_node(self, value):
self.nodes.add(value)
def add_edge(self, from_node, to_node, distance):
self.edges[from_node].append(to_node)
self.edges[to_node].append(from_node)
self.distances[(from_node, to_node)] = distance
self.distances[(to_node, from_node)] = distance
def dijkstra(graph, initial):
visited = {initial: 0}
path = {}
nodes = set(graph.nodes)
while nodes:
min_node = None
for node in nodes:
if node in visited:
if min_node is None:
min_node = node
elif visited[node] < visited[min_node]:
min_node = node
if min_node is None:
break
nodes.remove(min_node)
current_weight = visited[min_node]
for edge in graph.edges[min_node]:
weight = current_weight + graph.distances[(min_node, edge)]
if edge not in visited or weight < visited[edge]:
visited[edge] = weight
path[edge] = min_node
return visited, path
gp = Graph()
gp.add_node("A")
gp.add_node("B")
gp.add_node("C")
gp.add_node("D")
gp.add_node("E")
gp.add_edge("A", "C", 20)
gp.add_edge("A", "B", 10)
gp.add_edge("C", "D", 25)
gp.add_edge("D", "E", 5)
gp.add_edge("B", "D", 15)
(visited, path) = dijkstra(gp, 'A')
print ("Edges defaultDict: ",gp.edges)
print ("Nodes List: ",gp.nodes)
print ("-"*150)
print (visited, path)
\ No newline at end of file
../common/logo-hochschule-bochum-cvh-text.pdf
\ No newline at end of file
../common/logo-hochschule-bochum.pdf
\ No newline at end of file
../common/pgslides.sty
\ No newline at end of file
20180621/photo-20180621-145220.jpg

135 KiB

0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment