From 42f24a75b4c6bbe95c149be2b1c7f88ed44c0940 Mon Sep 17 00:00:00 2001 From: MYP Date: Mon, 19 Oct 2020 18:41:09 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E8=AF=BE=E7=A8=8B=E5=86=85?= =?UTF-8?q?=E5=AE=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../12 数组与稀疏矩阵.md | 710 ++++++++++++++++++ .../{12 字符串.md => 13 字符串.md} | 0 2 files changed, 710 insertions(+) create mode 100644 DataStructureAndAlgorithm/12 数组与稀疏矩阵.md rename DataStructureAndAlgorithm/{12 字符串.md => 13 字符串.md} (100%) diff --git a/DataStructureAndAlgorithm/12 数组与稀疏矩阵.md b/DataStructureAndAlgorithm/12 数组与稀疏矩阵.md new file mode 100644 index 0000000..43093c7 --- /dev/null +++ b/DataStructureAndAlgorithm/12 数组与稀疏矩阵.md @@ -0,0 +1,710 @@ +# 12 ϡ + +**֪ʶṹ** + +![ͼ1 ֪ʶṹ](https://img-blog.csdnimg.cn/20201019145851727.png) + +## 1. + +### 1.1 Ķ + +Ǿһ˳ϵɶɵļϣĶΪԪء + +磺 +- Ӧһά + +```math +A=(a_0,a_1,\cdots,a_{n-1}) +``` + +- Ӧά + +```math +A_{m \times n} =\begin{bmatrix} +a_{00}&a_{01} &\cdots &a_{0n-1}\\ +a_{10}&a_{11} &\cdots &a_{1n-1}\\ +\cdots& \cdots &\cdots &\cdots\\ +a_{m-10}&a_{m-11}& \cdots &a_{m-1n-1}\\ +\end{bmatrix} +``` + + +ʾȺĹԣͬһͣ + +±ʾĸԣռжĵԪ + + +### 1.2 Ĵ洢 + +**1`$n$`άĶ** + +±`$n$`ɵΪ`$n$`ά顣 + +磺 +```c +//һά飨ߣ +int[] a = new int[10]; + +//ά 棩 +int[ , ] a = new int[2,3]; + +//ά 壩ȣ飨壩2.ҳ 3. 4.С +int[ , , ] a = new int[2,3,4]; +``` + +**2洢ص** +- Ԫڴа˳洢 +- Ĵ洢䰴УCC++C#ȣУForturnȣС +- ʾ׵ַdz + +**3Ĵ洢** + +һά`a[n]` + +Ԫذ½DZδš + +`int[] a = new int[5];` + +![ͼ2 һά洢](https://img-blog.csdnimg.cn/20191218195949938.png) + +ά`a[m,n]` + +`int[ , ] a = new int[2,3];` + +![ͼ3 ά洢](https://img-blog.csdnimg.cn/20191218200106993.png) + + +ά`a[m,n,l]` + +һά±仯άһά±仯졣 + +`int[ , , ] a = new int[2,3,4];` + +![ͼ4 άĴ洢](https://img-blog.csdnimg.cn/20191218200209131.png) + +**ע⣺** + +C#`int[,]``int[][]` + +- `int[,]` ȷ + + +```c +static void Main(string[] args) +{ + + int[,] Arr = new int[2, 5] { { 1, 2, 3, 5, 6 }, { 1, 2, 3, 4, 5 } }; + + for (int i = 0; i < 2; ++i) + { + for (int j = 0; j < 5; ++j) + { + Console.Write(Convert.ToString(Arr[i, j]) + " "); + } + Console.WriteLine(); + } + // 1 2 3 5 6 + // 1 2 3 4 5 +} +``` + +- `int[][]` ȷ + +```c +static void Main(string[] args) +{ + + int[][] Arr = new int[3][]; //ʾһά + Arr[0] = new int[5] { 1, 2, 3, 4, 5 }; + Arr[1] = new int[2] { 0, 1 }; + Arr[2] = new int[0] { }; + + for (int i = 0; i < Arr.Length; ++i) + { + foreach (int j in Arr[i]) + { + Console.Write(j + " "); + } + Console.WriteLine(); + } + // 1 2 3 4 5 + // 0 1 + // +} +``` + +### 1.3 ķ + +ɷΪ̬Ͷ̬ࡣ + +**1̬** + +ڳʱռ顣 + + +``` +//̬飨֮鳤Ȳɸı䣩 +int[] a = new int[10]; +``` + +**2̬** + +ڳʱռ飨֮鳤ȿɸ + +![ͼ5 ̬ͼ](https://img-blog.csdnimg.cn/20201019105328214.png) + + +```c +using System; + +namespace LinearStruct +{ + /// + /// ̬ijʵ + /// + /// ̬Ԫص + public class DArray where T : IComparable + { + private T[] _array; + + /// + /// ȡ̬ĵǰ + /// + public int Size { get; private set; } + + /// + /// ʼDArrayʵ + /// + /// ̬ijʼ + public DArray(int size) + { + if (size <= 0) + throw new ArgumentOutOfRangeException(); + + Size = size; + _array = new T[Size]; + } + + /// + /// ı䶯̬ij + /// + /// ̬³ + public void ReSize(int newSize) + { + if (newSize <= 0) + throw new ArgumentOutOfRangeException(); + + if (Size != newSize) + { + T[] newArray = new T[newSize]; + int n = newSize < Size ? newSize : Size; + for (int i = 0; i < n; i++) + { + newArray[i] = _array[i]; + } + _array = newArray; + Size = newSize; + } + } + + /// + /// ȡָԪ + /// + /// ҪûõԪش㿪ʼ + /// ָԪ + public T this[int index] + { + get + { + if (index < 0 || index > Size - 1) + throw new IndexOutOfRangeException(); + return _array[index]; + } + set + { + if (index < 0 || index > Size - 1) + throw new IndexOutOfRangeException(); + _array[index] = value; + } + } + } +} +``` + +### 1.4 ̬Ӧ + +дһδ룬Ҫһ`N`ö̬`A``2~N`֮57ı顣 + +磺`N=100` + +5 7 10 14 15 20 21 25 28 30 35 40 42 45 49 50 55 56 60 63 65 70 75 77 80 84 85 90 91 95 98 100 + +ο£ + +![ͼ6 ̬ʵ](https://img-blog.csdnimg.cn/20201019105841792.png#pic_center) + +```c +static void Main(string[] args) +{ + Console.WriteLine("N="); + string s = Console.ReadLine(); + int n; + if (int.TryParse(s, out n)) + { + DArray arr = new DArray(10); + int j = 0; + for (int i = 2; i <= n; i++) + { + if (i % 5 == 0 || i % 7 == 0) + { + if (j == arr.Size) + arr.ReSize(arr.Size + 10); + + arr[j] = i; + j++; + } + } + for (int i = 0; i < j; i++) + { + Console.Write(arr[i] + " "); + } + } +} +``` + +--- +## 2. ϡ + + +### 2.1 ϡĶ + +**1ϡĶ** + +`$A$`зԪصĸԶԶСԪصĸ`$A$`Ϊϡ + + + +```math +A = \begin {bmatrix} +50 & 0 & 0 & 0\\ +10 & 0 & 20 & 0\\ +0 & 0 & 0 & 0\\ +-30 & 0 & -60 & 5 +\end{bmatrix} +``` + +öά洢̫˷Ѵ洢ռ䡣 + + +**2ϡIJ** + +- ȡ +- ȡ +- ȡָԪ +- ӷ +- ת +- ˷ + + + + +![ͼ7 ӿ](https://img-blog.csdnimg.cn/20201019111305663.png) + +```c +namespace LinearStruct +{ + /// + /// ij + /// + public interface IMatrix + { + /// + /// ȡ + /// + int Rows { get; } + + /// + /// ȡ + /// + int Cols { get; } + + /// + /// ȡָԪ + /// + /// ҪȡõԪش㿪ʼ + /// ҪȡõԪش㿪ʼ + /// + T this[int i, int j] { get; set; } + + /// + /// ӷ + /// + /// ֮ӵһ + /// Ӻ¾ + IMatrix Add(IMatrix b); + + /// + /// ת + /// + /// תú¾ + IMatrix Transpose(); + + /// + /// ˷ + /// + /// ֮ҳ˵һ + /// ˺¾ + IMatrix Multiply(IMatrix b); + } +} +``` + +### 2.2 ϡĴ洢ʵ + +`RowColValue`ķʽ洢ͶλϡеķԪأִ洢ϡķʽΪԪ飨Triple + + +![ͼ8 ϡĴ洢](https://img-blog.csdnimg.cn/20201019111609268.png) + +˳ķʽд洢 + +![ͼ9 ˳ʽ洢ϡ](https://img-blog.csdnimg.cn/20201019111704277.png) + +ʽķʽд洢 + +![ͼ10 ʽʽ洢ϡ](https://img-blog.csdnimg.cn/20201019111738648.png) + +**1Խķװ** + +![ͼ11 ϡͼ](https://img-blog.csdnimg.cn/20201019143656996.png) + +```c +using System; + +namespace LinearStruct +{ + /// + /// ϡ + /// + public class Triple : IComparable + { + /// + /// ȡھе + /// + public int Row { get; } + + /// + /// ȡھе + /// + public int Col { get; } + + /// + /// ȡýھеֵ + /// + public double Value { get; set; } + + /// + /// ʼTripleʵ + /// + /// ھе + /// ھе + /// ھеֵ + public Triple(int i, int j, double value) + { + if (i < 0 || j < 0) + throw new Exception("ԪλЧ."); + + Row = i; + Col = j; + Value = value; + } + + /// + /// Tripleַ + /// + /// Tripleַ + public override string ToString() + { + return string.Format("({0},{1},{2})", Row, Col, Value); + } + + /// + /// ȽԪݵĴС + /// + /// ȽϵԪ + /// + public int CompareTo(Triple other) + { + if (Value < other.Value) return -1; + if (Value > other.Value) return 1; + return 0; + } + } +} +``` + + +**2ϡķװ** + +![ͼ12 ϡͼ](https://img-blog.csdnimg.cn/20201019114927840.png) + + +```c +using System; + +namespace LinearStruct +{ + /// + /// ͵ʵ--ϡ + /// + public class SparseMatrix : IMatrix + { + private readonly DLinkList _lst; + + /// + /// ȡϡ + /// + public int Rows { get; } + + /// + /// ȡϡ + /// + public int Cols { get; } + + /// + /// ʼSparseMatrixʵ + /// + /// ϡ + /// ϡ + public SparseMatrix(int rows, int cols) + { + if (rows <= 0 || cols <= 0) + throw new ArgumentOutOfRangeException(); + Rows = rows; + Cols = cols; + _lst = new DLinkList(); + } + + private DNode GetIndex(int i, int j) + { + DNode temp = _lst.PHead; + while (temp != null) + { + if (temp.Data.Row == i && temp.Data.Col == j) + break; + temp = temp.Next; + } + return temp; + } + + private void RemoveNode(DNode node) + { + if (node.Next == null) + { + _lst.Remove(_lst.Length - 1); + } + else if (node.Prior == null) + { + _lst.Remove(0); + } + else + { + node.Prior.Next = node.Next; + node.Next.Prior = node.Prior; + } + } + + /// + /// ȡָԪ + /// + /// ҪȡõԪش㿪ʼ + /// ҪȡõԪش㿪ʼ + /// + public double this[int i, int j] + { + get + { + if (i < 0 || i > Rows - 1 || j < 0 || j > Cols - 1) + throw new IndexOutOfRangeException(); + DNode node = GetIndex(i, j); + + return node == null ? 0.0 : node.Data.Value; + } + set + { + if (i < 0 || i > Rows - 1 || j < 0 || j > Cols - 1) + throw new IndexOutOfRangeException(); + + DNode node = GetIndex(i, j); + + if (node == null) + { + if (value != 0.0) + { + _lst.InsertAtRear(new Triple(i, j, value)); + } + } + else + { + if (value != 0.0) + { + node.Data.Value = value; + } + else + { + RemoveNode(node); + } + } + } + } + + /// + /// ӷ + /// + /// ֮ӵһ + /// Ӻ¾ + public SparseMatrix Add(SparseMatrix b) + { + if (b == null) + throw new ArgumentNullException(); + if (b.Rows != Rows || b.Cols != Cols) + throw new Exception("."); + + SparseMatrix temp = new SparseMatrix(Rows, Cols); + for (int i = 0; i < Rows; i++) + for (int j = 0; j < Cols; j++) + temp[i, j] = this[i, j] + b[i, j]; + return temp; + } + + /// + /// ת + /// + /// תú¾ + public SparseMatrix Transpose() + { + SparseMatrix temp = new SparseMatrix(Cols, Rows); + for (int i = 0; i < temp.Rows; i++) + for (int j = 0; j < temp.Cols; j++) + temp[i, j] = this[j, i]; + return temp; + } + + /// + /// ˷ + /// + /// ֮ҳ˵һ + /// ˺¾ + public SparseMatrix Multiply(SparseMatrix b) + { + if (b == null) + throw new ArgumentNullException(); + if (Cols != b.Rows) + throw new Exception("."); + + SparseMatrix temp = new SparseMatrix(Rows, b.Cols); + for (int i = 0; i < Rows; i++) + { + for (int j = 0; j < b.Cols; j++) + { + double value = 0.0; + for (int k = 0; k < Cols; k++) + value += this[i, k] * b[k, j]; + + temp[i, j] = value; + } + } + return temp; + } + + /// + /// ӷ + /// + /// һ + /// ڶ + /// Ӻ¾ + public static SparseMatrix operator +(SparseMatrix a, SparseMatrix b) + { + if (a == null || b == null) + throw new ArgumentNullException(); + return a.Add(b); + } + + /// + /// ˷ + /// + /// ߵľ + /// ұߵľ + /// ˺¾ + public static SparseMatrix operator *(SparseMatrix a, SparseMatrix b) + { + if (a == null || b == null) + throw new ArgumentNullException(); + + return a.Multiply(b); + } + + /// + /// SparseMatrixַ + /// + /// SparseMatrixַ + public override string ToString() + { + string str = string.Empty; + DNode temp = _lst.PHead; + while (temp != null) + { + str += temp.Data + "\n"; + temp = temp.Next; + } + return str; + } + + IMatrix IMatrix.Add(IMatrix b) + { + return Add((SparseMatrix)b); + } + + IMatrix IMatrix.Transpose() + { + return Transpose(); + } + + IMatrix IMatrix.Multiply(IMatrix b) + { + return Multiply((SparseMatrix)b); + } + } +} +``` + + +```c +static void Main(string[] args) +{ + IMatrix a = new SparseMatrix(2, 3); + IMatrix b = new SparseMatrix(3, 2); + SparseMatrix c = new SparseMatrix(2, 2); + a[0, 2] = 1; + a[1, 0] = 1; + b[1, 1] = 4; + b[2, 0] = 1; + c[0, 1] = 1; + c[1, 0] = 1; + SparseMatrix d = (SparseMatrix)a * (SparseMatrix)b + c; + IMatrix e = a.Multiply(b).Add(c); + Console.WriteLine("D:\n{0}", d); + Console.WriteLine("E:\n{0}", e); + + // D: + // (0, 0, 1) + // (0, 1, 1) + // (1, 0, 1) + // E: + // (0, 0, 1) + // (0, 1, 1) + // (1, 0, 1) +} +``` + + diff --git a/DataStructureAndAlgorithm/12 字符串.md b/DataStructureAndAlgorithm/13 字符串.md similarity index 100% rename from DataStructureAndAlgorithm/12 字符串.md rename to DataStructureAndAlgorithm/13 字符串.md